2

更新/挿入/削除/テーブル作成から結果を取得する方法を見つけるのに苦労しています...

SSMS (SQL Server Management Studio) メッセージ タブに表示されるように取得したいと思います (ツールは done_in_proc だと思います)。

現時点では、例外を取得でき、SQL スクリプトで実行された出力をキャプチャできます。

私はすでに ExecuteScalar、ExecuteReader、ExecuteWithResults を試しましたが、何も得られませんでした。

ありがとう、

コード:

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();
        }
    }
}
4

0 に答える 0