【问题标题】:C# Handle on SQL Server Message Output [duplicate]SQL Server消息输出上的C#句柄[重复]
【发布时间】:2011-12-01 14:33:32
【问题描述】:

在 SQL Server Management Studio 中执行脚本时,通常会生成显示在消息窗口中的消息。例如在运行数据库备份时:

10% 已处理。

20% 已处理。

等等……

为数据库“Sample”处理了 1722608 个页面,文件 1 上的文件“Sampe”。

100% 已处理。

为数据库“Sample”处理了 1 页,文件 1 上的文件“Sample_Log”。

BACKUP DATABASE 在 202.985 成功处理了 1722609 个页面 秒(66.299 MB/秒)。

我希望能够在针对数据库运行 SQL 脚本的 C# 应用程序中显示这些消息。但是,我无法弄清楚如何处理生成的 SQL 消息输出。有人知道怎么做这个吗?我必须使用哪种连接框架对我来说并不重要。我对 LINQ、NHibernate、Entity Framework、ADO.Net、Enterprise Library 比较熟悉,并且很高兴学习新的。

【问题讨论】:

    标签: c# sql-server data-access-layer ssms


    【解决方案1】:

    这是我尝试过的示例代码,它适用于我。 http://www.dotnetcurry.com/ShowArticle.aspx?ID=344

    请注意,您需要的代码实际上是这部分:

    cn.Open();
    cn.InfoMessage += delegate(object sender, SqlInfoMessageEventArgs e)
    {                                    
             txtMessages.Text += "\n" + e.Message;                                   
    };
    

    是e.Message不断将消息返回给txtMessages(您可以替换为TextBox或Label)。

    您也可以参考这篇文章: Backup SQL Server Database with progress

    我的代码示例如下:

    //The idea of the following code is to display the progress on a progressbar using the value returning from the SQL Server message. 
    //When done, it will show the final message on the textbox. 
    String connectionString = "Data Source=server;Integrated Security=SSPI;";
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    
    public void DatabaseWork(SqlConnection con)
    {
        con.FireInfoMessageEventOnUserErrors = true;
        //con.InfoMessage += OnInfoMessage;
        con.Open();
        con.InfoMessage += delegate(object sender, SqlInfoMessageEventArgs e)
        {
            //Use textBox due to textBox has Invoke function. You can also utilize other way. 
            this.textBox.Invoke(
                (MethodInvoker)delegate()
                {
                    int num1;
                    //Get the message from e.Message index 0 to the length of first ' '
                    bool res = int.TryParse(e.Message.Substring(0, e.Message.IndexOf(' ')), out num1);
    
                    //If the substring can convert to integer
                    if (res)
                    {
                        //keep updating progressbar
                        this.progressBar.Value = int.Parse(e.Message.Substring(0, e.Message.IndexOf(' ')));
                    }
                    else
                    {
                        //Check status from message 
                        int succ;
                        succ = textBox.Text.IndexOf("successfully");
                        //or succ = e.Message.IndexOf("successfully");  //get result from e.Message directly
                        if (succ != -1) //If IndexOf find nothing, it will return -1
                        {
                            progressBar.Value = 100;
                            MessageBox.Show("Done!");
                        }
                        else
                        {
                            progressBar.Value = 0;
                            MessageBox.Show("Error, backup failed!");
                        } 
                    }
                }
            );
        };
        using (var cmd = new SqlCommand(string.Format(
            "Your SQL Script"//,
            //QuoteIdentifier(databaseName),
            //QuoteString(Filename)//,
            //QuoteString(backupDescription),
            //QuoteString(backupName)
            ), con))
        {
            //Set timeout = 1200 seconds (equal 20 minutes, you can set smaller value for shoter time out. 
            cmd.CommandTimeout = 1200;
            cmd.ExecuteNonQuery();
        }
        con.Close();
        //con.InfoMessage -= OnInfoMessage;
        con.FireInfoMessageEventOnUserErrors = false;
    }
    

    为了让进度条正常工作,您需要使用后台工作程序来实现这一点,这样您的应用程序就不会冻结并突然 100% 完成。

    【讨论】:

      【解决方案2】:

      SqlConnection.InfoMessage 事件在 SQL Server 返回警告或信息性消息时发生。 This website 展示了一种可能的实现方式。

      【讨论】:

        猜你喜欢
        • 2017-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        相关资源
        最近更新 更多