数据库连接的多种方式(三)

时间:2022-01-11 13:36:28
public static string ConnectionString { get { return CFunc.ConnectionString; //return System.Configuration.ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString; } } //wen public static int ExecuteSqlCommand(SqlCommand sqlCommand) { using (SqlConnection conn = new SqlConnection(CData.ConnectionString)) { sqlCommand.Connection = conn; conn.Open(); int result = sqlCommand.ExecuteNonQuery(); conn.Close(); return result; } } public static object ExecuteScalar(SqlCommand sqlCommand) { using (SqlConnection conn = new SqlConnection(CData.ConnectionString)) { sqlCommand.Connection = conn; conn.Open(); object obj = sqlCommand.ExecuteScalar(); conn.Close(); return obj; } } public static int ExecuteSql(string sql) { using (SqlCommand cmd = GetSqlCommand(sql)) { cmd.Connection.Open(); int i = cmd.ExecuteNonQuery(); cmd.Connection.Close(); return i; } } ///// <summary> ///// 执行带参数SQL语句 ///// </summary> ///// <param name="sql"></param> ///// <param name="cmdParms"></param> ///// <returns></returns> //public static int ExecuteNonQuery(string sql, params SqlParameter[] cmdParms) { // using (SqlConnection connection = new SqlConnection(ConnectionString)) { // using (SqlCommand cmd = new SqlCommand(sql, connection)) { // cmd.Parameters.AddRange(cmdParms); // return cmd.ExecuteNonQuery(); // } // } //} //wen public static SqlCommand GetSqlCommand(string Sql) { System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection(CData.ConnectionString); SqlCommand Cmd = new SqlCommand(Sql, Conn); return Cmd; } public static SqlDataReader GetSqlDataReader(string Sql) { SqlCommand Cmd = GetSqlCommand(Sql); SqlDataReader DataReader = null; try { Cmd.Connection.Open(); DataReader = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } catch (Exception e) { Cmd.Connection.Close(); CFunc.ResponseException(e.Message); } return DataReader; } public static SqlDataReader GetSqlDataReader(string sql, params SqlParameter[] sqlParameter) { SqlCommand cmd = GetSqlCommand(sql); cmd.Parameters.AddRange(sqlParameter); SqlDataReader dataReader = null; try { cmd.Connection.Open(); dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } catch (Exception e) { cmd.Connection.Close(); throw e; } return dataReader; } public static System.Data.DataSet GetDataSet(string Sql) { SqlDataAdapter Ada = new SqlDataAdapter(); Ada.SelectCommand = GetSqlCommand(Sql); System.Data.DataSet ds = new System.Data.DataSet(); try { Ada.SelectCommand.Connection.Open(); Ada.Fill(ds); Ada.SelectCommand.Connection.Close(); } catch (Exception e) { Ada.SelectCommand.Connection.Close(); CFunc.ResponseException(e.Message); } return ds; } public static System.Data.DataSet GetDataSet(SqlCommand sqlCommand) { using (SqlConnection conn = new SqlConnection(CData.ConnectionString)) { sqlCommand.Connection = conn; SqlDataAdapter da = new SqlDataAdapter(sqlCommand); conn.Open(); DataSet ds = new DataSet(); da.Fill(ds); conn.Close(); return ds; } } public static System.Data.DataTable GetDataTable(string Sql) { System.Data.DataSet ds = GetDataSet(Sql); System.Data.DataTable Dt = ds.Tables[0]; ds.Dispose(); return Dt; } public static object ExecuteScalar(string Sql) { SqlCommand Cmd = GetSqlCommand(Sql); object Obj = null; try { Cmd.Connection.Open(); Obj = Cmd.ExecuteScalar(); } catch (Exception e) { Cmd.Connection.Close(); throw e; } Cmd.Connection.Close(); return Obj; } public static int ExecuteNonQuery(string Sql) { SqlCommand Cmd = GetSqlCommand(Sql); int i = -1; try { Cmd.Connection.Open(); i = Cmd.ExecuteNonQuery(); Cmd.Connection.Close(); } catch (Exception e) { Cmd.Connection.Close(); CFunc.ResponseException(e.Message + " > "); } return i; } public static int ExecuteNonQuery(string sql, SqlParameter[] sqlParameters) { SqlCommand cmd = GetSqlCommand(sql); //foreach (SqlParameter SP in Parameters) //{ // Cmd.Parameters.Add(SP); //} cmd.Parameters.AddRange(sqlParameters); int i = -1; try { cmd.Connection.Open(); i = cmd.ExecuteNonQuery(); cmd.Connection.Close(); } catch (Exception e) { cmd.Connection.Close(); //CFunc.ResponseException(e.Message + " > " + sql); throw e; } return i; } public static string[] GetRecordOne(string StrSql, int ColumnNum) { string[] Record = new string[ColumnNum]; System.Data.SqlClient.SqlDataReader Dr = GetSqlDataReader(StrSql); if (Dr.Read()) { for (int i = 0; i < ColumnNum; i++) { Record[i] = Dr[i].ToString(); } } else { //CFunc.ResponseException(StrSql); Record = null; } Dr.Close(); return Record; }