使用SqlInfoMessageEventArgs时出现问题

时间:2014-11-20 07:21:03

标签: c# sql

在C#中,SqlInfoMessageEventArgs多次抛出相同的错误/消息。

例如:

请参阅以下输出,不确定消息多次显示的原因:

=== Processing file: [C:\D-Drive\Tasks\CIChanges\DB\Temp\DummyTable2.sql] ===
CREATE TABLE [dbo].[DummyTable2](
    [DI_CI_Version] [nvarchar](50) NOT NULL,
    [DI_DB_CI_Version] [nvarchar](50) NOT NULL
) ON [PRIMARY]

Msg: There is already an object named 'DummyTable2' in the database.
Msg: There is already an object named 'DummyTable2' in the database.

这是我的代码:

private static int ExecuteSQLFile(string SQLFileToExecute, StreamWriter logfile, SqlConnection Connection, int success, string ExitOnError)
{
    try
    {
        if (File.Exists(SQLFileToExecute))
        {
            string script = File.ReadAllText(SQLFileToExecute.Replace("\\", "\\\\"));
            Logging.WriteToLog(" ", logfile);
            Logging.WriteToLog("== Executing file: [" + SQLFileToExecute + "] ==", logfile);

            // split script on GO command
            IEnumerable<string> commandStrings = Regex.Split(script, @"^\s*GO\s*$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
            Connection.Open();
            Connection.InfoMessage += delegate(object sender, SqlInfoMessageEventArgs e)
            {
                //Logging.WriteToLog("Msg: "  + e.Message, logfile);
                logfile.WriteLine("Msg: " + e.Message);
                success = 1;
                if (!File.Exists("err.log")) File.Create("err.log");
            };

            foreach (string commandString in commandStrings)
            {
                if (commandString.Trim() != "")
                {
                    success = 0;
                    Console.WriteLine(commandString.ToString());
                    logfile.WriteLine(commandString.ToString());
                    new SqlCommand(commandString, Connection).ExecuteNonQuery();
                    if (success == 0)
                    {
                        Logging.WriteToLog("Command executed successfully.", logfile);
                    }
                    if (success == 1 && ExitOnError == "true")
                    {
                        Connection.Close();
                        return 1;
                    }
                }
            }

            Connection.Close();
            return success;
        }
        else
        {
            Console.WriteLine("File doesnot exists..." + SQLFileToExecute);
            return 1;
        }
    }
    catch (Exception e)
    {
        Logging.WriteToLog("Unable to execute file: " + SQLFileToExecute + " Execution failed with error: \n" + e.Message, logfile);
        throw e;
    }
}

有人可以找出问题的根源,为什么错误/消息会多次显示。

如果您还需要其他信息,请告诉我。

修改 我的DummyTable1.sql文件的内容:

CREATE TABLE [dbo].[DummyTable1](
    [DI_CI_Version] [nvarchar](50) NOT NULL,
    [DI_DB_CI_Version] [nvarchar](50) NOT NULL)
ON [PRIMARY]
GO 

我的DummyTable2.sql文件的内容:

CREATE TABLE [dbo].[DummyTable2](
    [DI_CI_Version] [nvarchar](50) NOT NULL,
    [DI_DB_CI_Version] [nvarchar](50) NOT NULL)
ON [PRIMARY]
GO 

我的SQLFileToExecute的内容:

<?xml version="1.0"?>
<PACKAGE>
    <Execute File="C:\D-Drive\Tasks\CIChanges\DB\Temp\DummyTable1.sql"/>
    <Execute File="C:\D-Drive\Tasks\CIChanges\DB\Temp\DummyTable2.sql"/>
</PACKAGE>

0 个答案:

没有答案