在C#中的调用 比如说按钮点击是调用的是 StoreProc方法
public int StoreProc(){
--连接数据库
using (SqlConnection Connection = new SqlConnection(ConnectionString))
{Connection.Open(); --打开连接
SqlCommand cmd = new SqlCommand(); --执行SQL语句的
cmd.Connection = Connection;
SqlParameter[] parameters = {
new SqlParameter("@num1", SqlDbType.int),
new SqlParameter("@num2", SqlDbType.int),
new SqlParameter("@result", SqlDbType.Int)
};
parameters[0].Value = 10;
parameters[1].Value = 2;
parameters[2].Direction = ParameterDirection.ReturnValue;
cmd.CommandType = CommandType.StoredProcedure; -- 指定执行存储过程
cmd.Parameters.Clear();
cmd.Parameters.AddRange(parameters);
cmd.CommandText = "usp_wmh"; -- -- 存储过程的名称。
cmd.ExecuteNonQuery();
return int.Parse(parameters[2].Value.ToString());
}
}
cmd.CommandType 类:
using System; | ||||
namespace System.Data | ||||
{ | ||||
// 摘要: | ||||
// 指定如何解释命令字符串。 | ||||
public enum CommandType | ||||
{ | ||||
// 摘要: | ||||
// SQL 文本命令。(默认。) | ||||
Text = 1, | ||||
// | ||||
// 摘要: | ||||
// 存储过程的名称。 | ||||
StoredProcedure = 4, | ||||
// | ||||
// 摘要: | ||||
// 表的名称。 | ||||
TableDirect = 512, | ||||
} | ||||
} |