C#中连接SQL Server数据库、执行存储过程

时间:2022-11-02 21:48:29
应用命名空间:

using System.Data.SqlClient;
using System.Configuration;


执行Select语句:

 private void button2_Click(object sender, EventArgs e)
{

SqlConnection con = new SqlConnection("server=服务器名;database=数据库名;pwd=密码;uid=登录名");
con.Open();
//SqlDataAdapter sda = new SqlDataAdapter("select name,age from hang",con);
SqlDataAdapter sda = new SqlDataAdapter("select name,age from hang", con);
DataSet ds = new DataSet();
sda.Fill(ds, "hang");
dataGridView1.DataSource = ds.Tables[0];
}


执行没有参数的存储过程:

 private void button1_Click(object sender, EventArgs e)
{
<pre name="code" class="csharp"> SqlConnection con = new SqlConnection("server=服务器名;database=数据库名;pwd=密码;uid=登录名");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "hanghang";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource=ds.Tables[0];
//SqlDataReader sdr = cmd.ExecuteReader();
//sdr.Read();
//if (sdr.HasRows)
//{
// label1.Text = "suolong";

//}
}


使用配置文件连接数据库:先在项目中添加一个APP.config文件,添加如下节点

  <connectionStrings>
    <add name="myconn" connectionString="server=服务器名;database=数据库名;pwd=密码;uid=登录名"/>
  </connectionStrings>


执行没有参数的存储过程:

 private void button3_Click(object sender, EventArgs e)
{
string connstr = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection con = new SqlConnection(connstr);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "hanghang";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}


执行带输入参数的存储过程:

 private void button4_Click(object sender, EventArgs e)
{
string mycon=ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection con = new SqlConnection(mycon);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "hh";
cmd.Parameters.Add(new SqlParameter("@name",SqlDbType.VarChar,10));
cmd.Parameters["@name"].Value = "a";
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}


执行带有输出参数的存储过程:

 private void button5_Click(object sender, EventArgs e)
{
string mycon = ConfigurationManager.ConnectionStrings["myconn"].ConnectionString;
SqlConnection con = new SqlConnection(mycon);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "hhang";
cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 10));
cmd.Parameters.Add("@age", SqlDbType.Int);
cmd.Parameters["@age"].Direction = ParameterDirection.Output;
cmd.Parameters["@name"].Value = "a";
SqlDataReader dr = cmd.ExecuteReader();
int c = Convert.ToInt32(cmd.Parameters["@age"].Value);
label1.Text = Convert.ToString(c);
}


C#中连接SQL Server数据库、执行存储过程