與當前連接關聯的交易已完成但尚未處理。事務必須在連接可用於執行SQL語句之前進行處理。爲什麼突然出現Connection Dispose錯誤?
protected string installDatabase(string connectionString, bool createSampleData,string scriptpath)
{
//uncomment this line to support transactions
using (var scope = new System.Transactions.TransactionScope())
{
string scriptsFolder = scriptpath + "\\install\\Scripts";
string createDatabaseFile = string.Format(@"{0}\{1}", scriptsFolder, "Campus.sql");
string error = proceedSQLScripts(createDatabaseFile, connectionString);
if (!String.IsNullOrEmpty(error))
{
return error;
}
scope.Complete();
}
return "true";
}
protected string proceedSQLScripts(string pathToScriptFile, string connectionString)
{
List<string> statements = new List<string>();
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
using (Stream stream = File.OpenRead(pathToScriptFile))
using (StreamReader reader = new StreamReader(stream))
{
string statement = string.Empty;
while ((statement = readNextStatementFromStream(reader)) != null)
{
statements.Add(statement);
}
}
try
{
foreach (string stmt in statements)
{
{
SqlCommand command = new SqlCommand(stmt, conn);
command.ExecuteNonQuery();
//conn.Close();
}
}
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
conn.Close();
}
return string.Empty;
}
請加上語言標記。 – Jens 2015-02-06 06:46:05
*你有哪些錯誤? – 2015-02-06 08:09:13
string error = proceedSQLScripts(createDatabaseFile,connectionString);在這裏我得到了這個錯誤! (與當前連接相關的事務已經完成但尚未處理,事務必須在連接可用於執行SQL語句之前進行處理。) – 2015-02-09 06:37:08