SSMS
 sql >> Database >  >> Database Tools >> SSMS

Handle C# nell'output del messaggio di SQL Server

Ecco il codice di esempio che ho provato e funziona per me.http://www. dotnetcurry.com/ShowArticle.aspx?ID=344

Nota che il codice che ti serve è in realtà questa parte:

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

È che e.Message continua a restituire il messaggio a txtMessages (puoi sostituirlo come TextBox o Label).

Puoi anche fare riferimento a questo articolo:Backup del database di SQL Server con progresso

Un esempio del mio codice è il seguente:

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

Per far funzionare la barra di avanzamento, è necessario implementarlo con un backgroundworker, che l'applicazione non si bloccherà e verrà eseguita all'improvviso al 100%.