1、 #region 公用方法 public static int GetMaxID(string Field, string Table) string strsql = select max( + Field+ )+1 from + Table; object obj = GetSingle(strsql); if (obj = null) return 1; else return int.Parse(obj.ToString(); public static bool Exists(string strS, params SqlParameter cmdParms) object obj
2、= GetSingle(strS, cmdParms); int cmdr; if (Object.Equals(obj, null) | (Object.Equals(obj, System.DBNull.Value) cmdr = 0; cmdr = int.Parse(obj.ToString(); if (cmdr = 0) return false; return true; #endregion #region 执行简单SQL语句 / SQL语句returns影响的记录数 public static int ExecuteSql(string SQLStr) using (SqlC
3、onnection connection = new SqlConnection(connectionStr) using (SqlCommand cmd = new SqlCommand(SQLStr, connection) try connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; catch (System.Data.SqlClient.SqlException E) connection.Close(); throw new Exception(E.Message); / 执行多条SQL语句,实现数据库事
4、务。SQLStringList多条SQL语句 public static void ExecuteSqlTran(ArrayList SQLStringList) using (SqlConnection conn = new SqlConnection(connectionStr) conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; for (int n = 0; n 1
5、) cmd.CommandText = strsql; cmd.ExecuteNonQuery(); tx.Commit(); tx.Rollback(); / 执行带一个存储过程参数的的SQL语句。content参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加 public static int ExecuteSql(string SQLStr, string content) SqlCommand cmd = new SqlCommand(SQLStr, connection); System.Data.SqlClient.SqlParameter myParame
6、ter = new System.Data.SqlClient.SqlParameter(content, SqlDbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); finally cmd.Dispose(); / 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)strSQLfs图像字节,数据库的字段类型为image的情况 public static int ExecuteSqlInsertImg(string strSQL, byte fs) using (SqlConnectio
7、n connection = new SqlConnection(connectionString) SqlCommand cmd = new SqlCommand(strSQL, connection);fs, SqlDbType.Image); myParameter.Value = fs; / 执行一条计算查询结果语句,返回查询结果(object)。计算查询结果语句查询结果(object) public static object GetSingle(string SQLStr) object obj = cmd.ExecuteScalar(); return null; return
8、obj; catch (System.Data.SqlClient.SqlException e) throw new Exception(e.Message); / 执行查询语句,返回SqlDataReader查询语句SqlDataReader public static SqlDataReader ExecuteReader(string strSQL) SqlConnection connection = new SqlConnection(connectionStr); SqlDataReader myReader = cmd.ExecuteReader(); return myRea
9、der; / 执行查询语句,返回DataSetDataSet public static DataSet Query(string SQLStr) DataSet ds = new DataSet(); SqlDataAdapter command = new SqlDataAdapter(SQLStr, connection); command.Fill(ds, ds catch (System.Data.SqlClient.SqlException ex) throw new Exception(ex.Message); return ds; #region 执行带参数的SQL语句 pub
10、lic static int ExecuteSql(string SQLStr, params SqlParameter cmdParms) using (SqlCommand cmd = new SqlCommand() PrepareCommand(cmd, connection, null, SQLStr, cmdParms); cmd.Parameters.Clear();SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter) public static void ExecuteSqlTran(Hashtable SQLStringList) using
11、 (SqlTransaction trans = conn.BeginTransaction() /循环 foreach (DictionaryEntry myDE in SQLStringList) string cmdText = myDE.Key.ToString(); SqlParameter cmdParms = (SqlParameter)myDE.Value; PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); trans.Commit(); catch tra
12、ns.Rollback(); throw; public static object GetSingle(string SQLStr, params SqlParameter cmdParms) public static SqlDataReader ExecuteReader(string SQLStr, params SqlParameter cmdParms) public static DataSet Query(string SQLStr, params SqlParameter cmdParms) using (SqlDataAdapter da = new SqlDataAdap
13、ter(cmd) da.Fill(ds, private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter cmdParms) if (conn.State != ConnectionState.Open) cmd.CommandText = cmdText; if (trans != null) cmd.Transaction = trans; cmd.CommandType = CommandType.Text;/
14、cmdType; if (cmdParms ! foreach (SqlParameter parm in cmdParms) cmd.Parameters.Add(parm); #region 存储过程操作 / 执行存储过程storedProcName存储过程名parameters存储过程参数 public static SqlDataReader RunProcedure(string storedProcName, IDataParameter parameters) SqlConnection connection = new SqlConnection(connectionString); SqlDataReader returnReader; SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.CommandType = CommandType.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1