【问题标题】:Run stored procedure in C#, pass parameters and capture the output result在 C# 中运行存储过程,传递参数并捕获输出结果
【发布时间】:2011-10-13 20:23:51
【问题描述】:

这是一个我想要完成的简单任务,但 ASP.NET 使它变得相当困难,几乎不可能。我跟着这个问题 Running a Stored Procedure in 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

我正在捕捉它。

【问题讨论】:

  • 您的问题与 ASP.NET 有什么关系?这是您不了解 ADO.NET 的问题。花点时间阅读msdn.microsoft.com/en-us/library/system.data.sqlclient.aspx
  • 对于这种简单的事情,我必须了解ADO.net,有关于ADO.NET的书籍:)
  • 阅读文档而不是根据您不理解的示例进行编码真的不会有什么坏处。

标签: c# asp.net sql-server-2005 stored-procedures ado.net


【解决方案1】:
  1. 如果您确实需要来自结果集中的数据,请不要使用 SqlCommand.@987654321@

  2. 确保您的程序使用set nocount on

  3. 然后使用SqlCommand.@987654323@

    return (int)myCommand.ExecuteScalar(); // value of select @@rowcount
    

编辑:至于你的参数:

myCommand.Parameters.AddWithValue("@username","jsmith");
myCommand.Parameters.AddWithValue("@month","January");

【讨论】:

  • 这将应用于此代码stackoverflow.com/questions/4124100/… ?
  • 更新为包含参数信息。
  • 所以到目前为止它正在工作,但现在我需要更改参数值并再次调用 ExecuteScalar。新参数名称为@Type。你能帮忙吗?
  • 或者,您可以关闭 nocount,省略 select @@rowcount 并使用 ExecuteNonQuery 的返回值作为您的行数。
【解决方案2】:

我更喜欢使用 linq-to-sql 来处理存储过程。创建一个 linq-to-sql 模型,在其中添加要调用的 SP。这会将 SP 公开为生成的数据上下文中的函数,其中参数是普通的 C# 函数。返回的值将作为 C# 对象的集合公开。

如果您有多个来自 SP 的结果,事情会变得有点复杂,但仍然非常简单。

【讨论】:

    【解决方案3】:

    使用命令的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();
      }
    }
    

    【讨论】:

    • 不更正这一行:“DbType.VarChar”。这必须是“SqlDbType.VarChar”
    【解决方案4】:
    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); 
    }
    

    【讨论】:

    • 该值作为结果返回,而不是返回值。
    【解决方案5】:

    从 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);
    

    【讨论】:

    • 该值作为结果返回,而不是返回值。此外,您需要提供与命令的连接才能使该代码正常工作。
    • 该值作为结果返回,而不是返回值。
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多