【IT168技术文档】
这一篇我们介绍不带参数的存储过程执行方法的代码,它调用通用数据访问类(SqlHelper)执行 SqlHelper.ExecuteNonQuery()方法,使用示例为;
int val = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, SQLexec, null);
其中传递的4个参数:
“conn”—为链接字符;
“ CommandType.StoredProcedure”—为SQL命令类型。这里表示执行SQL存储过程形式;
“SQLexec”—为SQL存储过程字符;
“null”—是以数组形式提供SqlCommand命令中用到的参数列表,这里不是以数据形式,所以为空。
当ExecuteNonQuery()执行 select,结果总是返回-1,ExecuteNonQuery()对于 Update、Insert 和 Delete 语句,返回值为该命令所影响的行数。protected void btnExecuteProc_Click(object sender, EventArgs e) { //获取要执行的存储过程名 string SQLexec = "sp_getGISnews"; SqlCommand cmd = new SqlCommand(); //定义对象资源保存的范围,一量using范围结束,将释放对方所占的资源 using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction)) //注意因为conn已经在SqlHelper.cs中使用,这里不能重名所以改用conn1 { //打开链接 conn.Open(); //调用执行方法,因为没有参数,所以最后一项设为null int val = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, SQLexec, null); Response.Write(" "); } }