using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ICS.Data { public class SqlCommandHelper { public static void CmdExecuteNonQuery(string sql, SqlCommand cmd) { try { cmd.CommandText = sql; int count = cmd.ExecuteNonQuery(); if (count <= 0) { throw new Exception("受影响行数小于0"); } cmd.Parameters.Clear(); sql = string.Empty; } catch (Exception ex) { throw new Exception(ex.Message); } } public static void DeleteCmdExecuteNonQuery(string sql, SqlCommand cmd) { try { cmd.CommandText = sql; int count = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); sql = string.Empty; } catch (Exception ex) { throw new Exception(ex.Message); } } public static void CmdExecuteNonQuery(string sql, SqlParameter[] sp, SqlCommand cmd) { try { foreach (SqlParameter parameter in sp) { if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } cmd.Parameters.Add(parameter); } cmd.CommandText = sql; int count = cmd.ExecuteNonQuery(); if (count <= 0) { throw new Exception("受影响行数小于0"); } cmd.Parameters.Clear(); sql = string.Empty; } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 事物取DataTable /// /// /// /// public static DataTable SQlReturnData(string SQl, SqlCommand cmd) { DataTable dt = new DataTable(); SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter(); cmd.CommandText = SQl; dr.SelectCommand = cmd; dr.Fill(dt); return dt; } public static DataSet SQlReturnDataSet(string SQl, SqlCommand cmd) { DataSet dt = new DataSet(); SqlDataAdapter dr = new System.Data.SqlClient.SqlDataAdapter(); cmd.CommandText = SQl; dr.SelectCommand = cmd; dr.Fill(dt); return dt; } } }