网络工作室暑假后第二次培训资料(SQLServer存储过程和ADO.NET访问存储过程)整理(二)
使用ADO,NET来调用存储过程可以分为一下4种方式
1)调用不带参数的存储过程
2)调用带一个参数的存储过程
3)调用带多个参数的存储过程
4)调用带输出参数的存储过程
(本代码示例所使用的存储过程,使用的是作者的上一篇博文所创建的存储过程,博文地址:http://blog.csdn.net/yisuowushinian/article/details/8045481,请大家参考)
一,ADO.NET调用不带参数的存储过程,使用的存储过程是上篇博文示例1-1创建的存储过程
具体的调用代码1-1演示如下:
/// <summary> /// 调用带返回值的存储过程 /// </summary> public void ShowData4() { SqlConnection con = new SqlConnection(strConString); SqlCommand cmd = new SqlCommand("select_MathResult", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] parameter = { new SqlParameter("@activityName",SqlDbType.NVarChar,50), new SqlParameter("@result",SqlDbType.Int,4) }; parameter[0].Value = "比武招亲"; //设定输出参数的输出方法向 parameter[1].Direction = ParameterDirection.Output; cmd.Parameters.AddRange(parameter); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable table = new DataTable(); adapter.Fill(table); Response.Write(Convert.ToInt32(cmd.Parameters[1].Value)); }这样就完成了ADO.NET对Sql Server存储过程的访问的所有示例。因为在整理的时候发现内容太多,无法在一篇博文完成,只能分为若干篇。请大家继续关注我的博客,下次将为大家更新,Sql Server的表连接查询,多表查询,分页,等知识。