using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace RS.DBUtility { public class SQLServerHelper { //数据库连接字符串 public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectString"].ConnectionString; public SqlConnection SqlConn; #region 数据库基本操作 public SQLServerHelper() { SqlConn = new SqlConnection(ConnectionString); } public SQLServerHelper(string conn) { SqlConn = new SqlConnection(conn); } public void Dispose() { Dispose(true); GC.SuppressFinalize(true); } protected void Dispose(bool disposing) { if (disposing) { return; } if (SqlConn != null) { SqlConn.Dispose(); SqlConn = null; } } /// /// 打开数据库连接 /// public void DBConnection() { try { //确认是否已打开连接,为打开则打开连接 if (this.SqlConn.State != ConnectionState.Open) { SqlConn.Open(); } } catch (Exception ex) { throw ex; } } /// /// 关闭数据库连接 /// public void DBClose() { try { //确认是否已打开连接,为打开则打开连接 if (this.SqlConn.State == ConnectionState.Open) { SqlConn.Close(); } } catch (Exception ex) { throw ex; } } /// /// 获得查询结果 /// /// SQL字符串 /// public DataTable QuerySql(string sqlstr) { try { DataTable dt = null; try { SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, ConnectionString); try { dt = new DataTable(); sqlda.Fill(dt); } catch { sqlda.MissingMappingAction = MissingMappingAction.Ignore; } } catch (SqlException ex) { throw ex; } return dt; } catch (Exception ex) { throw ex; } } /// /// 获得查询结果 /// /// SQL字符串 /// public DataTable QuerySql(string sqlstr, string conn) { try { DataTable dt = null; SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, conn); dt = new DataTable(); sqlda.Fill(dt); return dt; } catch (Exception ex) { throw ex; } } /// /// 操作数据库(无返回值) /// /// 操作SQL字符串 public int ExecuteNonQuerySQL(string sqlstr) { int i = 0; try { //确认是否已打开连接,为打开则打开连接 if (this.SqlConn.State != ConnectionState.Open) { SqlConn.Open(); } SqlCommand sqlcommand = new SqlCommand(sqlstr, this.SqlConn); i = sqlcommand.ExecuteNonQuery(); return i; } catch (SqlException ex) { throw ex; } finally { if (this.SqlConn.State != ConnectionState.Closed) { SqlConn.Close(); } } } /// /// 操作数据库(无返回值) /// /// 操作SQL字符串 public bool ExecuteBNonQuerySQL(string sqlstr, bool hostConnect = false) { try { if (!PingHelper.PingConnectString(ConnectionString)) return false; using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); SqlCommand sqlcommand = new SqlCommand(sqlstr, conn); sqlcommand.ExecuteNonQuery(); return true; } } catch (SqlException ex) { ex.ToString(); throw; } } /// /// 操作数据库(无返回值),调用该方法前要先打开数据库 /// /// 操作SQL字符串 /// 操作类型:true:已打开数据库;false:数据库关闭 public void ExecuteNonQuerySQL(string sqlstr, bool IsOpen) { try { if (IsOpen) { SqlCommand sqlcommand = new SqlCommand(sqlstr, this.SqlConn); sqlcommand.ExecuteNonQuery(); } } catch (Exception ex) { throw ex; } } /// /// 判断是否存在数据 /// /// SQL字符串 /// 查询结果 public bool b_QuerySql(string sqlstr) { try { SqlCommand command = new SqlCommand(sqlstr, this.SqlConn); SqlDataReader sqlda = command.ExecuteReader(CommandBehavior.CloseConnection); command.Parameters.Clear(); return sqlda.GetBoolean(0); } catch (Exception ex) { DBClose(); throw ex; } } /// /// 批量插入数据 /// /// 数据库中数据表名 /// 数据源(DataTable) public void BulkInsert(string TableName, DataTable dt) { try { SqlConn.Open(); using (SqlBulkCopy bulkcopy = new SqlBulkCopy(this.SqlConn)) { bulkcopy.DestinationTableName = TableName; try { bulkcopy.WriteToServer(dt); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } catch (SqlException ex) { throw ex; } finally { SqlConn.Close(); } } /// /// 处理存储过程并返回结果 /// /// 存储过程名称 /// 存储过程使用的命令参数 /// 返回结果数据表 public DataTable GetProceResult(string procName, SqlParameter[] parameters) { try { // 确认数据库是否已打开 if (this.SqlConn.State != ConnectionState.Open) { SqlConn.Open(); } SqlCommand command = new SqlCommand(procName, this.SqlConn); command.CommandType = CommandType.StoredProcedure; // 循环输入存储过程命令参数 if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } SqlParameter RtParameter = new SqlParameter(); RtParameter.Direction = ParameterDirection.ReturnValue; RtParameter.ParameterName = "ReturnValue"; RtParameter.SqlDbType = SqlDbType.Int; RtParameter.Size = 4; RtParameter.IsNullable = false; RtParameter.Precision = 0; RtParameter.Scale = 0; RtParameter.SourceColumn = string.Empty; RtParameter.SourceVersion = DataRowVersion.Default; RtParameter.Value = null; // 增加返回参数 command.Parameters.Add(RtParameter); DataTable dt = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(procName, this.SqlConn); adapter.SelectCommand = command; adapter.SelectCommand.CommandTimeout = 1000; adapter.Fill(dt); if (this.SqlConn.State == ConnectionState.Open) { SqlConn.Close(); } return dt; } catch (Exception ex) { throw ex; } } /// /// 处理存储过程(无返回值) /// /// 存储过程名 /// 存储过程命令参数 public void ExecuteProcedure(string procName, SqlParameter[] parameters) { // 确认数据库是否连接 if (this.SqlConn.State != ConnectionState.Open) { SqlConn.Open(); } try { SqlCommand command = new SqlCommand(procName, this.SqlConn); command.CommandType = CommandType.StoredProcedure; //反复输入命令参数 if (parameters != null) { foreach (SqlParameter parameter in parameters) { command.Parameters.Add(parameter); } } // 存储过程处理 command.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { if (this.SqlConn.State == ConnectionState.Open) { SqlConn.Close(); } } } public void ExecuteNonQuerySQLTran(List sqlstrlist) { //确认是否已打开连接,为打开则打开连接 if (this.SqlConn.State != ConnectionState.Open) { SqlConn.Open(); } SqlTransaction tran = SqlConn.BeginTransaction(); SqlCommand sqlcommand = SqlConn.CreateCommand(); sqlcommand.Transaction = tran; try { foreach (string sqlstr in sqlstrlist) { sqlcommand.CommandText = sqlstr; sqlcommand.ExecuteNonQuery(); } tran.Commit(); } catch (SqlException ex) { //Console.WriteLine(ex.Message); tran.Rollback(); throw new Exception(ex.Message.ToString()); } finally { if (this.SqlConn.State != ConnectionState.Closed) { SqlConn.Close(); } } } #endregion 数据库基本操作 } }