用c#运行存储过程,传递参数并捕获输出结果
本文关键字:输出 结果 参数 运行 存储过程 | 更新日期: 2023-09-27 18:11:16
这是一个简单的任务,我想实现,但ASP。NET使得它相当困难,几乎不可能。我跟着这个问题在c# Button中运行存储过程,但发现ExecuteNonQuery
没有返回查询的输出。
我尝试了另一种方法,但似乎无法以这种方式传递参数
SqlConnection myConnection = new SqlConnection(myconnectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandText = "usp_GetCustomer";
myCommand.SelectParameter <-- does not exist
可以有人写这个简单的代码,我怎么实现它?基本上,我将@username
和@month
(两个字符串)传递给存储过程,它返回一个我想要捕获并分配给标签控件的数字。
谢谢
我的查询的输出是这样的。它运行一个复杂的查询,创建一个临时表,然后运行
select @@rowcount
我正在捕捉它
-
如果你真的想要一个结果集中的数据,不要使用
SqlCommand.ExecuteNonQuery()
。 -
确保你的程序使用
set nocount on
-
然后使用
SqlCommand.ExecuteScalar()
return (int)myCommand.ExecuteScalar(); // value of select @@rowcount
编辑:至于你的参数:
myCommand.Parameters.AddWithValue("@username","jsmith");
myCommand.Parameters.AddWithValue("@month","January");
我更喜欢使用linq-to-sql来处理存储过程。创建一个link -to-sql模型,在其中添加要调用的SP。这将把SP作为生成的数据上下文中的函数公开,其中的参数是普通的c#函数。返回值将作为c#对象的集合公开。
如果你从SP中得到多个结果,事情就会变得有点复杂,但仍然很直接。
使用命令的Parameters
集合设置参数,使用ExecuteScalar
执行查询并从单行单列结果中获取值。
使用using
块来确保连接和命令在任何情况下都是关闭和正确处置的。注意,您必须提供到命令对象的连接:
int result;
using (SqlConnection connection = new SqlConnection(myconnectionString)) {
using (SqlCommand command = new SqlCommand(connection)) {
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "usp_GetCustomer";
command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
command.Parameters.Add("@month", SqlDbType.VarChar).Value = month;
connection.Open();
result = (int)myCommand.ExecuteScalar();
}
}
using(SqlConnection myConnection = new SqlConnection(myconnectionString))
{
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandText = "usp_GetCustomer";
myCommand.Parameters.Add("@USER_NAME", SqlDbType.VarChar).Value = sUserName; // user name that you pass to stored procedure
myCommand.Parameters.Add("@Month", SqlDbType.VarChar).Value = iMonth; // Month that you pass to stored procedure
// to get return value from stored procedure
myCommand.Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
myConnection .Open();
myCommand.ExecuteScalar();
// Returnvalue from stored procedure
return Convert.ToInt32(command.Parameters["@ReturnValue"].Value);
}
从SQL Server获取返回值的简单代码
SqlConnection myConnection = new SqlConnection(myconnectionString);
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandText = "usp_GetCustomer";
myCommand.Parameters.Add("@USER_NAME", SqlDbType.VarChar).Value = sUserName; // user name that you pass to the stored procedure
myCommand.Parameters.Add("@Month", SqlDbType.VarChar).Value = iMonth; //Month that you pass to the stored procedure
// to get return value from the stored procedure
myCommand.Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
myConnection .Open();
myCommand.ExecuteScalar();
// Returnvalue from the stored procedure
int iReturnValue = Convert.ToInt32(command.Parameters["@ReturnValue"].Value);