执行SQL语句时出错 [英] Error on execute SQL statements

查看:230
本文介绍了执行SQL语句时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误与当前连接关联的事务已完成但尚未处理。事务必须在连接可用于执行SQL语句之前处理。..?



Error "The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements."..?

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;
           }

           //if (createSampleData)   //visibility for checkbox sampledata is set to false as not required
           //{
           //    string createSampleDataFile = string.Format(@"{0}\{1}", scriptsFolder, "Campus_createSampleData.sql");
           //    error = proceedSQLScripts(createSampleDataFile, connectionString);
           //    if (!String.IsNullOrEmpty(error))
           //    {
           //        this.pnlLog.Visible = true;
           //        addResult(string.Format("An error occured: {0}", error));
           //        return false;
           //    }
           //}

           //uncomment this line to support transactions\

           scope.Complete();
           scope.Dispose();

       }
       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();
           conn.Dispose();

       }
       return string.Empty;
   }

推荐答案

似乎是事务获取超时。检查下面的链接,这个你出来



错误 - 与当前连接关联的事务已完成但尚未处理 [ ^ ]
Seems like transaction getting timeout. Check below link, this sort you out

Error - The transaction associated with the current connection has completed but has not been disposed[^]


这篇关于执行SQL语句时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆