从基础开始,从一个SQLHelper开始
最开始考虑的问题有这三点:
1.Access和SQLServer都要能用。
2.尽量简单,清晰。
3.性能不出大问题。
public class SQLHelp { #region 私有域 private static string ConnStr = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString; private static string ProviderType = ConfigurationManager.AppSettings["ProviderType"].ToString(); private DbConnection _Conn; private static DbProviderFactory DbFactory=DbProviderFactories.GetFactory(ProviderType); private DbCommand _Cmd = DbFactory.CreateCommand(); private string _CmdText; private CommandType _CmdType; #endregion #region 属性 private DbConnection Conn { get { return _Conn; } set { _Conn = value; } } public DbCommand Cmd { get { return _Cmd; } set { _Cmd = value; } } public string CmdText { get { return _CmdText; } set { _CmdText = value; _Cmd.CommandText = value; } } public CommandType CmdType { get { return _CmdType; } set { _CmdType = value; _Cmd.CommandType = value; } } #endregion #region 私有方法 private void OpenCon() { try { if (Conn == null) { Conn = DbFactory.CreateConnection(); } if (Conn.State == ConnectionState.Closed) { Conn.ConnectionString = ConnStr; Conn.Open(); } } catch (Exception ex) { throw (new Exception("打开数据库出错,错误" + ex.Message)); } } private void CloseCon() { if (Conn.State == ConnectionState.Open) { Conn.Close(); } } #endregion /// <summary> /// 执行指定的SQL语句(如添加、更新、删除等操作) /// </summary> /// <returns>受影响的行数</returns> public int ExecuteNonQuery() { OpenCon(); try { return Cmd.ExecuteNonQuery(); } catch (Exception ex) { throw (new Exception("数据库执行错误: " + ex.Message)); } finally { CloseCon(); } } /// <summary> /// 使用 DataAdapter 提取数据返回为 DataTable,并为 DataTable 指定名称 /// </summary> /// <returns>DataTable</returns> public DataTable ExecuteDataTable() { OpenCon(); try { using (DbDataAdapter da =DbFactory.CreateDataAdapter()) { da.SelectCommand = Cmd; DataTable dt = new DataTable(); da.Fill(dt); return dt; } } catch (Exception ex) { throw (new Exception("数据库执行错误:" + ex.Message)); } finally { CloseCon(); } }
}
}
写的过程发现了很多问题:每个属性该不该静态,赋初值在哪里进行,属性的写法是否合适,需不需要构造函数。最主要的疑惑:SQL语句执行完了立即释放Cmd好,还是等多条都用同一个Cmd执行好。
然后发邮件给一位学长,学长回复:
问题1、看场合决定用什么工厂,抽象工厂或工厂方法。
问题2、连接和命令都是用完立即释放。按你的做法,会一直占用数据库的连接资源,SQL SERVER的连接数本来就少,这种做法个人觉得不好。
问题3、这个问题干脆不回答了。我建议你看看ADO.NET的思想,考虑它为何提供连接后又提供断开连接。断开连接的产生就是为了让你读取完数据后马上释放连接资源。你反复连接是没问题的,因为有连接池在管理。
代码方面
异常处理我是在程序中作统一处理,辅助类中不加异常处理代码。
这是改后的第二版:
public class SQLHelp { #region 私有域 private static string ConnStr = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString; private static string ProviderType = ConfigurationManager.AppSettings["ProviderType"].ToString(); private DbConnection _Conn; private static DbProviderFactory DbFactory=DbProviderFactories.GetFactory(ProviderType); private DbCommand _Cmd; private string _CmdText; private CommandType _CmdType = CommandType.Text; #endregion #region 属性 private DbConnection Conn { get { return _Conn; } set { _Conn = value; } } public DbCommand Cmd { get { return _Cmd; } set { _Cmd = value; } } public string CmdText { get { return _CmdText; } set { _CmdText = value;} } public CommandType CmdType { get { return _CmdType; } set { _CmdType = value; } } #endregion #region 私有方法 private void OpenCon() { try { if (Conn == null) { Conn = DbFactory.CreateConnection(); } if (Conn.State == ConnectionState.Closed) { Conn.ConnectionString = ConnStr; Conn.Open(); } } catch (Exception ex) { throw (new Exception("打开数据库出错,错误" + ex.Message)); } } private void CloseCon() { if (Conn.State == ConnectionState.Open) { Conn.Close(); } if (Cmd != null) { Cmd.Dispose(); Cmd = null; } } private void StartCommend() { OpenCon(); Cmd = Conn.CreateCommand(); Cmd.CommandText = CmdText; Cmd.CommandType = CmdType; } #endregion /// <summary> /// 执行指定的SQL语句(如添加、更新、删除等操作) /// </summary> /// <returns>受影响的行数</returns> public int ExecuteNonQuery() { StartCommend(); try { return Cmd.ExecuteNonQuery(); } catch (Exception ex) { throw (new Exception("数据库执行错误: " + ex.Message)); } finally { CloseCon(); } } /// <summary> /// 使用 DataAdapter 提取数据返回为 DataTable,并为 DataTable 指定名称 /// </summary> /// <returns>DataTable</returns> public DataTable ExecuteDataTable() { StartCommend(); try { using (DbDataAdapter da =DbFactory.CreateDataAdapter()) { da.SelectCommand = Cmd; DataTable dt = new DataTable(); da.Fill(dt); return dt; } } catch (Exception ex) { throw (new Exception("数据库执行错误:" + ex.Message)); } finally { CloseCon(); } }
}
}
对有些概念依然模糊,需要每次执行完语句就直接断开链接吗?这里面的哪些写法不对?
求各位指教。