通过分装的方法:
public class SQLHelper { private static readonly string ConnectionString = ConfigurationManager.AppSettings["conn"].ToString(); //SqlParameter[] 方便传递数组 /// <summary> /// 主要用于封装Command对象的ExecuteNonQuery方法,用于数据的增删改 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回受影响的行数</returns> public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams) { SqlConnection conn = new SqlConnection(ConnectionString); SqlCommand comm = new SqlCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); try { return comm.ExecuteNonQuery(); } catch (SqlException ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 封装Command对象的ExecuteReader 方法用于数据的查询 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回SqlDataReader对象</returns> public static SqlDataReader ExcuteReader(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams) { SqlConnection conn = new SqlConnection(ConnectionString); SqlCommand comm = new SqlCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); try { //自动关闭 return comm.ExecuteReader(CommandBehavior.CloseConnection); } catch (SqlException ex) { throw ex; } } /// <summary> /// 封装Commond对象的ExecuteScalar方法,用于返回首行首列数据 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回的是object单一的值</returns> public static object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams) { SqlConnection conn = new SqlConnection(ConnectionString); SqlCommand comm = new SqlCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); try { return comm.ExecuteScalar(); } catch (SqlException ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 主要用于返回DataTable 查询的数据 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回DataTable对象</returns> public static DataTable GetDataTable(string cmdText, CommandType cmdType, params SqlParameter[] cmdParams) { SqlConnection conn = new SqlConnection(); SqlCommand comm = new SqlCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = comm; DataSet ds = new DataSet(); try { //自动打开自动关闭 实现断开式的链接 da.Fill(ds); return ds.Tables[0]; } catch (SqlException ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 主要用于给Command对象进行初始化赋值工作 /// </summary> /// <param name="comm">是操作的Comman对象</param> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> private static void PrepareCommand(SqlCommand comm, SqlConnection conn, string cmdText, CommandType cmdType, SqlParameter[] cmdParams) { if (conn.State == ConnectionState.Closed) conn.Open(); comm.Connection = conn; comm.CommandText = cmdText; comm.CommandType = cmdType; if (cmdParams != null) { for (int i = 0; i < cmdParams.Length; i++) { comm.Parameters.Add(cmdParams[i]); } } } }
Access数据库的帮助类:
public class AccessHelper { private static readonly string ConnectionString = ConfigurationManager.AppSettings["conn"].ToString(); //SqlParameter[] 方便传递数组 /// <summary> /// 主要用于封装Command对象的ExecuteNonQuery方法,用于数据的增删改 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回受影响的行数</returns> public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams) { OleDbConnection conn = new OleDbConnection(ConnectionString); OleDbCommand comm = new OleDbCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); try { return comm.ExecuteNonQuery(); } catch (OleDbException ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 封装Command对象的ExecuteReader 方法用于数据的查询 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回SqlDataReader对象</returns> public static OleDbDataReader ExcuteReader(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams) { OleDbConnection conn = new OleDbConnection(ConnectionString); OleDbCommand comm = new OleDbCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); try { //自动关闭 return comm.ExecuteReader(CommandBehavior.CloseConnection); } catch (OleDbException ex) { throw ex; } } /// <summary> /// 封装Commond对象的ExecuteScalar方法,用于返回首行首列数据 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回的是object单一的值</returns> public static object ExecuteScalar(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams) { OleDbConnection conn = new OleDbConnection(ConnectionString); OleDbCommand comm = new OleDbCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); try { return comm.ExecuteScalar(); } catch (OleDbException ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 主要用于返回DataTable 查询的数据 /// </summary> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> /// <returns>返回DataTable对象</returns> public static DataTable GetDataTable(string cmdText, CommandType cmdType, params OleDbParameter[] cmdParams) { OleDbConnection conn = new OleDbConnection(ConnectionString); OleDbCommand comm = new OleDbCommand(); PrepareCommand(comm, conn, cmdText, cmdType, cmdParams); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = comm; DataSet ds = new DataSet(); try { //自动打开自动关闭 实现断开式的链接 da.Fill(ds); return ds.Tables[0]; } catch (OleDbException ex) { throw ex; } finally { conn.Close(); } } /// <summary> /// 主要用于给Command对象进行初始化赋值工作 /// </summary> /// <param name="comm">是操作的Comman对象</param> /// <param name="conn">Connection对象</param> /// <param name="cmdText">Command.CommandText</param> /// <param name="cmdType">Command.CommandType</param> /// <param name="cmdParams">Command.Parameters</param> private static void PrepareCommand(OleDbCommand comm, OleDbConnection conn, string cmdText, CommandType cmdType, OleDbParameter[] cmdParams) { if (conn.State == ConnectionState.Closed) conn.Open(); comm.Connection = conn; comm.CommandText = cmdText; comm.CommandType = cmdType; if (cmdParams != null) { for (int i = 0; i < cmdParams.Length; i++) { comm.Parameters.Add(cmdParams[i]); } } } }
时间: 2024-12-13 20:45:18