以下为我常用数据库访问的帮助类:
View Code
using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections.Generic; public static class DBHelper { // connection string public static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); #region Exec sql with paramsters /// <summary> /// exec a sql, return quantity line it impact /// </summary> /// <param name="SQLString">SQL</param> /// <returns>inpacted line :val</returns> public static int ExecNonQuery(string SQLString, params SqlParameter[] cmdParms) { using (SqlCommand cmd = new SqlCommand()) { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); connection.Close(); return val; } } } /// <summary> /// exec a sql and reuturn the first line data。 /// </summary> /// <param name="SQLString"></param> /// <returns></returns> public static object ExecScalar(string SQLString, params SqlParameter[] cmdParms) { using (SqlCommand cmd = new SqlCommand()) { using (SqlConnection connection = new SqlConnection(connectionString)) { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); return obj; } } } public static int ExecText(string sql) { SqlConnection connection = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand( sql,connection); connection.Open(); int val = cmd.ExecuteNonQuery(); connection.Close(); return val; } /// <summary> /// exec sql ,return a sqldatareader object /// </summary> /// <param name="strSQL"></param> /// <returns>SqlDataReader</returns> public static SqlDataReader ExecReader(string SQLString, params SqlParameter[] cmdParms) { using (SqlCommand cmd = new SqlCommand()) { SqlConnection connection = new SqlConnection(connectionString); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return myReader; } catch { connection.Close(); throw; } } } /// <summary> /// exec sql return dataset /// </summary> /// <param name="SQLString"></param> /// <returns>DataSet</returns> public static DataSet ExecDataSet(string SQLString, params SqlParameter[] cmdParms) { using (SqlCommand cmd = new SqlCommand()) { using (SqlConnection connection = new SqlConnection(connectionString)) { PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch(SqlException e) { throw e; } return ds; } } } } private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Connection = conn; cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;//cmdType; if (cmdParms != null) { foreach (SqlParameter parameter in cmdParms) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } } } #endregion #region StoreProcedure /// <summary> /// exec storeproceducre SqlDataReader ( notice: don't forget close SqlDataReader after exec ) /// </summary> /// <param name="storedProcName"></param> /// <param name="parameters"></param> /// <returns>SqlDataReader</returns> public static SqlDataReader ExecStoreReader(string storedProcName, IDataParameter[] parameters) { SqlCommand cmd = new SqlCommand(); SqlConnection connection = new SqlConnection(connectionString); connection.Open(); try { cmd = BuildQueryCommand(connection, storedProcName, parameters); cmd.CommandType = CommandType.StoredProcedure; SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return myReader; } catch { connection.Close(); throw; } } /// <summary> /// exec storeprocedure return dataset /// </summary> /// <param name="storedProcName"></param> /// <param name="parameters"></param> /// <param name="tableName">DataSet's table</param> /// <returns>DataSet</returns> public static DataSet ExecStoreDataSet(string storedProcName, IDataParameter[] parameters) { using (DataSet ds = new DataSet()) { using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { sda.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters); sda.Fill(ds); return ds; } } } } /// <summary> /// exec storeprocedure reutn impacted line /// </summary> /// <param name="storedProcName"></param> /// <param name="parameters"></param> /// <param name="rowsAffected"></param> /// <returns></returns> public static int ExecStoreNonQuery(string storedProcName, IDataParameter[] parameters) { SqlCommand cmd = new SqlCommand(); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); cmd = BuildIntCommand(connection, storedProcName, parameters); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); //connection.Close(); return val; } } /// <summary> /// construct a SqlCommand object.(return a set of result but not a number) /// </summary> /// <param name="connection"></param> /// <param name="storedProcName"></param> /// <param name="parameters"></param> /// <returns>SqlCommand</returns> private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(storedProcName, connection); command.CommandType = CommandType.StoredProcedure; foreach (SqlParameter parameter in parameters) { if (parameter != null) { //check teh unsigned paramsters , and set it's value to DBNull.Value if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } command.Parameters.Add(parameter); } } return command; } /// <summary> /// create a sqlCommand object ,(return a integer value) /// </summary> /// <param name="storedProcName"></param> /// <param name="parameters"></param> /// <returns>SqlCommand </returns> private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command; } #endregion }