首页 > 代码库 > 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
- /// <summary>
- /// 添加实体
- /// </summary>
- /// <param name="sysCompany"></param>
- /// <param name="sysUser"></param>
- public bool InsertEntities(TSysCompany sysCompany, TSysUser sysUser)
- {
- using (teaCRMDBContext db = new teaCRMDBContext())
- {
- if (db.Connection.State != ConnectionState.Open)
- {
- db.Connection.Open();
- }
- var tran = db.Connection.BeginTransaction();
- try
- {
- db.TSysCompanies.Insert(sysCompany);
- db.TSysUsers.Insert(sysUser);
- tran.Commit();
- return true;
- }
- catch (Exception ex)
- {
- tran.Rollback();
- throw new Exception(ex.Message);
- return false;
- }
- finally
- {
- if (db.Connection.State != ConnectionState.Closed)
- {
- db.Connection.Close();
- }
- }
- }
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。