SSMS
 sql >> Cơ Sở Dữ Liệu >  >> Database Tools >> SSMS

Xử lý C # trên Đầu ra Thông báo Máy chủ SQL

Đây là mã ví dụ tôi đã thử và nó phù hợp với tôi. http:// www. dotnetcurry.com/ShowArticle.aspx?ID=344

Lưu ý rằng mã bạn cần thực sự là phần này:

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

Đó là e.Message tiếp tục trả lại tin nhắn trở lại txtMessages (Bạn có thể thay thế bằng TextBox hoặc Label).

Bạn cũng có thể tham khảo bài viết này: Sao lưu cơ sở dữ liệu SQL Server với tiến trình

Ví dụ về mã của tôi như sau:

//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;
}

Để thanh tiến trình hoạt động, bạn cần thực hiện điều này với một công việc nền, ứng dụng của bạn sẽ không bị đóng băng và hoàn thành 100% đột ngột.



  1. DBeaver
  2.   
  3. phpMyAdmin
  4.   
  5. Navicat
  6.   
  7. SSMS
  8.   
  9. MySQL Workbench
  10.   
  11. SQLyog
  1. SQL Server Management Studio:nhắc nhập người dùng

  2. Bộ kết quả đặt SSIS từ luồng dữ liệu thành biến

  3. Cú pháp không chính xác gần 'for' SQL Server

  4. Cách hiển thị các giá trị trong Col3 trong đó các giá trị Col1 là đúng và các giá trị sai trong Col1 chỉ hiển thị NULL trong Col3

  5. Việc xóa 'OPTIMIZE_FOR_SEQUENTIAL_KEY' có giải quyết được lỗi script.sql của tôi hay liên quan nhiều hơn (SQL Server Express DB -> SQL Server) không?