У меня есть метод, который выполняет некоторые файлы .sql на C#. При выполнении первого файла он работает нормально. Но когда он пытается выполнить следующий файл (см. условие else в методе ExecuteFileBasedOnSequence), он выдает исключение. Я попробовал выполнить сценарий Sql на SQL Server, и он работает отлично.
РЕДАКТИРОВАТЬ:
Внутреннее исключение говорит следующее
"ОШИБКА [42000] [Microsoft][Драйвер ODBC SQL Server][SQL
Сервер]Неверный синтаксис рядом с надписью "Go".
Но сценарий работает нормально при работе на Sql Server.
Вот код C#,
Код: Выделить всё
private void ExecuteFileBasedOnSequence(string fileToExecute, bool executeSQLBySequence = true)
{
m_DBConnection.BeginTransaction();
if (executeSQLBySequence)
{
//Read each line of the file into a stringbuilder object to be executed individually (may throw, should just pitch out of this function)
using (StreamReader srSQL = new StreamReader(fileToExecute))
{
//Read each line of the file into a stringbuilder object to be executed individually (may throw, should just pitch out of this function)
string sqlLine;
StringBuilder sqlString = new StringBuilder();
while (!srSQL.EndOfStream)
{
sqlLine = srSQL.ReadLine();
if (string.IsNullOrEmpty(sqlLine) == false)
{
// We don't actually execute the "GO" lines but can use them to determine when to call the executenonquery function
if (string.Compare(sqlLine, "GO", true) == 0)
{
// Make sure we have something to execute
if (string.IsNullOrEmpty(sqlString.ToString()) == false)
{
m_DBConnection.ExecuteNonQuery(sqlString.ToString());
}
sqlString.Clear();
}
// Add the next line to the stringbuilder object
else
{
sqlString.AppendLine(sqlLine);
}
}
}
}
}
else
{
using (StreamReader srSQL = new StreamReader(fileToExecute))
{
if (m_DBConnection != null)
{
m_DBConnection.Open();
m_DBConnection.ExecuteNonQuery(srSQL.ReadToEnd()); //THIS ONE FAILS.
}
}
}
m_DBConnection.CommitTransaction();
}
Код: Выделить всё
ExecuteFileBasedOnSequence(@"Database\UpdateTriggersToSQL2017.sql");//This one works
ExecuteFileBasedOnSequence(@"Database\UpgradePharmSpecScripts.sql", false); //This one is failing
Код: Выделить всё
Use PharmSpecDB
Go
--PC1725E-590
IF NOT EXISTS ( SELECT 23 FROM [PharmSpecDB].[dbo].[tblActivityLogMaster] WHERE Lower(strActivityLog) Like 'user rights%' AND LOWER(strActivityLogDesc) Like 'user rights%')
Begin
ALTER TABLE [PharmSpecDB].[dbo].[tblActivityLogMaster] DISABLE TRIGGER I_TRG_ACTIVITYLOGMASTER
Insert into [PharmSpecDB].[dbo].[tblActivityLogMaster](strActivityLog,bDeleted,strActivityLogDesc) values ('User rights modified', 0, 'User rights modified')
ALTER TABLE [PharmSpecDB].[dbo].[tblActivityLogMaster] ENABLE TRIGGER I_TRG_ACTIVITYLOGMASTER
End
"ExecuteNonQuery": свойство текста команды не инициализировано."
Подробнее здесь: https://stackoverflow.com/questions/790 ... d-sql-serv