【问题标题】:Window Service - waiting for stored procedure to get the resultsWindow Service - 等待存储过程得到结果
【发布时间】:2012-09-25 12:54:15
【问题描述】:

我正在从C# Window Service 运行一个存储过程。存储过程是一个非常繁重的查询,需要很长时间。

我想等到存储过程完成并返回值。

有什么方法可以确定存储过程完成了吗?

【问题讨论】:

  • 您现在运行的程序如何?发布您的代码

标签: c# sql-server-2008 sql-server-2005 c#-4.0 windows-services


【解决方案1】:

使用Threading.Tasks。以下代码将使您的线程等待任务完成。

public void CallStoredProcMethod()
{
    var task1 = System.Threading.Tasks.Task.Factory.StartNew(() => RunStoredPro());

    // thread will wait there till the operation finish
    task1.Wait();
}

public void RunStoredPro()
{
    using (var connection = new SqlConnection(sqlConnString))
    {
        // your database call
    }
}

【讨论】:

    【解决方案2】:

    System.Data.Common.DbCommand 有一个 CommandTimeout 属性。

    获取或设置在终止尝试执行之前的等待时间 命令并产生错误。等待命令执行的时间(以秒为单位)。实现者请注意,建议 0 表示无超时。

    请注意,SqlCommand 的默认值为 30 秒。如果将 CommandTimeout 属性设置为 0,则调用将花费存储过程执行所需的时间。 SqlCommand 上 ExecuteScalar 实现的 msdn 页面具有以下示例,稍作修改以设置 CommandTimeout

    static public int AddProductCategory(string newName, string connString)
    {
        Int32 newProdID = 0;
        string sql =
            "INSERT INTO Production.ProductCategory (Name) VALUES (@Name); "
            + "SELECT CAST(scope_identity() AS int)";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
    
            // this will cause the command to wait until the sproc is finished
            cmd.CommandTimeout = 0; 
    
            cmd.Parameters.Add("@Name", SqlDbType.VarChar);
            cmd.Parameters["@name"].Value = newName;
            try
            {
                conn.Open();
                newProdID = (Int32)cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        return (int)newProdID;
    }
    

    【讨论】:

      【解决方案3】:

      在存储过程中使用return语句并获取值

      例如作为存储过程

      set ANSI_NULLS ON
      set QUOTED_IDENTIFIER ON
      go
      
      ALTER PROCEDURE [dbo].[Validate]
      @inputdata varchar(50),
      @outputdata varchar(50) output
      
      AS
      SET @outputdata = (
      SELECT TOP 1 Password FROM dbo.tblUser WHERE Login = @a)
      
      RETURN @outputdata
      GO
      

      我这里简单解释一下C#中的核心代码

             string returnValue = string.Empty;
               ...............
              SqlConn.Open();
              sqlcomm.CommandType = CommandType.StoredProcedure;
              SqlParameter param = new SqlParameter("@inputdata", SqlDbType.VarChar);
              param.Direction = ParameterDirection.Input;
              param.Value = Username;
              sqlcomm.Parameters.Add(param);
      
              SqlParameter retval = sqlcomm.Parameters.Add("@outputdata", SqlDbType.VarChar);
              retval.Direction = ParameterDirection.Output;
              string retunvalue = (string)sqlcomm.Parameters["@outputdata"].Value;
               ......................
               .........
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-17
        相关资源
        最近更新 更多