C# SQL output to text like SSMS message tab

為{幸葍}努か 提交于 2019-12-08 02:16:01

问题


I'm having some trouble finding a way to get the results from updates/inserts/deletes/table creates...

I would like to get it like you see in the SSMS (SQL Server Management Studio) message tab (guess the tool is done_in_proc).

At the moment, I can get the exceptions, and I can capture the prints done in the SQL script.

I already tried ExecuteScalar, ExecuteReader, ExecuteWithResults and got nothing.

Thanks,

Code:

public void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
    foreach (SqlError err in e.Errors)
    {

        richTextBoxDeployCopy.AppendText("Info : "+err.Message + Environment.NewLine);
    }
}        

public void ExecSQLScript(string Instance, string Database, string SQLScript, string Username, string Password)
{

    if (Application2Deploy == "DUMMY")
    {
        server = "Server";
    }
    else
    {
        server = Instance;
    } string sqlConnectionString = @"User ID=" + Username + "; Password = " + Password + ";Persist Security Info=False;Initial Catalog=" + Database + ";Data Source=" + server + "\\" + Instance;

    FileInfo file = new FileInfo(SQLScript);

    string script = file.OpenText().ReadToEnd();

    conn = new SqlConnection(sqlConnectionString);

    conn.FireInfoMessageEventOnUserErrors = true;

    Server SQLserver = new Server(new ServerConnection(conn));

    if (checkBoxDeployCopyTestingScript.Checked)
    {
        richTextBoxDeployCopy.AppendText("Test: Running SQL Script......" + Environment.NewLine);
        LogFile(DateTime.Now + "Test: Running SQL Script......", LogPath);
    }
    else
    {
        try
        {
            SQLserver.ConnectionContext.Connect();
            SQLserver.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage);
            SQLserver.ConnectionContext.BeginTransaction();
            SQLserver.ConnectionContext.ExecuteNonQuery(script);

            Error = false;
        }
        catch (Exception SQLEx)
        {
            Error = true;
            richTextBoxDeployCopy.AppendText("Error executing SQL Script." + Environment.NewLine);
            LogFile(DateTime.Now + "Error executing SQL Script", LogPath);
            richTextBoxDeployCopy.AppendText("Exception: " + SQLEx.InnerException.Message + Environment.NewLine);
            LogFile(DateTime.Now + "Exception: " + SQLEx.InnerException.Message, LogPath); 
            try
            {
                SQLserver.ConnectionContext.RollBackTransaction();
            }
            catch (Exception ex2)
            {
                richTextBoxDeployCopy.AppendText("Error executing rollback." + Environment.NewLine);
                richTextBoxDeployCopy.AppendText("Exception: " + ex2.Message + Environment.NewLine);
            }
        }
        if (Error == false)
        {
            SQLserver.ConnectionContext.CommitTransaction();
            CopyExit("End");
            file.OpenText().Close();
            file.OpenText().Dispose();
            SQLserver.ConnectionContext.SqlConnectionObject.Close();
            //SQLserver.ConnectionContext.ForceDisconnected();
        }
        else
        {
            CopyExit("Abort");
            file.OpenText().Close();
            file.OpenText().Dispose();
            SQLserver.ConnectionContext.SqlConnectionObject.Close();
            //SQLserver.ConnectionContext.ForceDisconnected();
        }
    }
}

来源:https://stackoverflow.com/questions/22101625/c-sharp-sql-output-to-text-like-ssms-message-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!