ADO.NET访问SQL Server调用存储过程

存储过程详见上一篇文章。

1、无参存储结构

SqlConnection con = new SqlConnection(conStr);
SqlCommand com = con.CreateCommand();
com.CommandText = "StuProc";
com.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
com.ExecuteNonQuery(); //返回受影响的行数
con.Close();

2、带参数的存储结构,返回数据集

SqlConnection con = new SqlConnection(conStr);
SqlCommand com = con.CreateCommand();
com.CommandText = "Test1";
com.CommandType = System.Data.CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("@mobile", "1234567890"));
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter sad = new SqlDataAdapter(com);
sad.Fill(ds);
DataTable dt = ds.Tables[0];
con.Close();

3、带有输出参数的存储结构

SqlConnection con = new SqlConnection(conStr);
SqlCommand com = con.CreateCommand();
com.CommandText = "Test2";
com.CommandType = System.Data.CommandType.StoredProcedure;
com.Parameters.Add(new SqlParameter("@mobile", "17621733756"));//入参
SqlParameter p1 = new SqlParameter("@IsRight", System.Data.SqlDbType.Int, 20);//出参
//标明输出方向
p1.Direction = System.Data.ParameterDirection.Output;
com.Parameters.Add(p1);
con.Open();
com.ExecuteNonQuery();
con.Close();

通过p1.value获取输出的参数值

参考:https://www.cnblogs.com/ylbtech/p/2851448.html

猜你喜欢

转载自blog.csdn.net/PLF_1994/article/details/80663231