首页 > 代码库 > ServerVersion异常,无效操作。连接被关闭。解决方案

ServerVersion异常,无效操作。连接被关闭。解决方案

【版权声明:本文为特维博客原创,未经准许谢绝转载。如需转载,请务必在转载时注明本博客地址。】

问题在于:State Closed System.Data.ConnectionState
连接没有open().
在try中加入
if (oleDbConnection1.State != ConnectionState.Open)
    oleDbConnection1.Open();
加上
finally
{
    if (oleDbConnection1.State != ConnectionState.Close)
    {
        oleDbConnection1.Close();
    }
}

 

详见http://bbs.csdn.net/topics/90347542

 

 
  1. /// <summary>  
  2.         /// 添加实体  
  3.         /// </summary>  
  4.         /// <param name="sysCompany"></param>  
  5.         /// <param name="sysUser"></param>  
  6.         public bool InsertEntities(TSysCompany sysCompany, TSysUser sysUser)  
  7.         {  
  8.             using (teaCRMDBContext db = new teaCRMDBContext())  
  9.             {  
  10.                 if (db.Connection.State != ConnectionState.Open)  
  11.                 {  
  12.                     db.Connection.Open();  
  13.                 }  
  14.                 var tran = db.Connection.BeginTransaction();  
  15.                 try 
  16.                 {  
  17.                     db.TSysCompanies.Insert(sysCompany);  
  18.                     db.TSysUsers.Insert(sysUser);  
  19.                     tran.Commit();  
  20.                     return true;  
  21.                 }  
  22.                 catch (Exception ex)  
  23.                 {  
  24.                     tran.Rollback();  
  25.                     throw new Exception(ex.Message);  
  26.                     return false;  
  27.                       
  28.                 }  
  29.                 finally 
  30.                 {  
  31.                     if (db.Connection.State != ConnectionState.Closed)  
  32.                     {  
  33.                         db.Connection.Close();  
  34.                     }  
  35.                 }  
  36.             }  
  37.         }