与当前连接关联的事务已完成但尚未处理。必须先处理事务,然后才能使用连接执行SQL语句。
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;
}
答案 0 :(得分:0)
尝试以下代码
Finally
{
conn.Close();
conn.Dispose();
}