using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace Car.Command { public static class SQLHelper { //获取连接字符串 public static string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString; #region ExecuteTable /// <summary> /// 返回相应的数据表(无参数) /// </summary> /// <param name="text">要执行的语句,SQL语句或者是存储过程</param> /// <param name="type">要执行的类型,SQL语句或者是存储过程</param> /// <returns></returns> public static DataTable ExecuteTable(string text, CommandType type) { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(ConString)) { try { con.Open(); SqlDataAdapter adp = new SqlDataAdapter(text, con); adp.SelectCommand.CommandType = type; adp.Fill(dt); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示"); } } return dt; } /// <summary> /// 返回相应的数据表(重载,使用参数) /// </summary> /// <param name="text">要执行的语句,SQL语句或者是存储过程</param> /// <param name="para">使用的参数</param> /// <param name="type">要执行的类型,SQL语句或者是存储过程</param> /// <returns></returns> public static DataTable ExecuteTable(string text, SqlParameter[] para, CommandType type) { DataTable dt = new DataTable(); using (SqlConnection con = new SqlConnection(ConString)) { try { con.Open(); SqlDataAdapter adp = new SqlDataAdapter(text, con); adp.SelectCommand.CommandType = type; adp.SelectCommand.Parameters.AddRange(para); adp.Fill(dt); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示"); } } return dt; } #endregion #region ExecuteNonQuery /// <summary> /// 返回受影响的行数(不使用参数) /// </summary> /// <param name="text">要执行的SQL语句或者是存储过程</param> /// <param name="type">执行SQL语句或者是存储过程</param> /// <returns></returns> public static int ExecuteNonQuery(string text, CommandType type) { int i = 0; using (SqlConnection con = new SqlConnection(ConString)) { try { con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = text; cmd.CommandType = type; i = cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示"); } } return i; } /// <summary> /// 返回受影响的行数(重载,使用参数) /// </summary> /// <param name="text">要执行的SQL语句或者是存储过程</param> /// <param name="para">要使用的参数</param> /// <param name="type">执行SQL语句或者是存储过程</param> /// <returns></returns> public static int ExecuteNonQuery(string text, SqlParameter[] para, CommandType type) { int i = 0; using (SqlConnection con = new SqlConnection(ConString)) { try { con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = text; cmd.Parameters.AddRange(para); cmd.CommandType = type; i = cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message, "提示"); } } return i; } #endregion #region ExcuteReader /// <summary> /// 返回SqlDataReader数据集(不使用参数) /// </summary> /// <param name="text"></param> /// <param name="type"></param> /// <returns></returns> public static SqlDataReader ExcuteReader(string text, CommandType type) { SqlConnection con = new SqlConnection(ConString); using (SqlCommand cmd = new SqlCommand(text, con)) { con.Open(); cmd.CommandType = type; SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return sdr; } } /// <summary> /// 重载,返回SqlDataReader数据集(使用参数) /// </summary> /// <param name="text"></param> /// <param name="para"></param> /// <param name="type"></param> /// <returns></returns> public static SqlDataReader ExcuteReader(string text, SqlParameter[] para, CommandType type) { SqlConnection con = new SqlConnection(ConString); using (SqlCommand cmd = new SqlCommand(text, con)) { cmd.CommandType = type; cmd.Parameters.AddRange(para); SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); return sdr; } } #endregion #region ExcuteScalar /// <summary> /// 返回数据集第一行第一列的值 /// </summary> /// <param name="text"></param> /// <param name="type"></param> /// <returns></returns> public static object ExcuteScalar(string text, CommandType type) { object obj = null; try { using (SqlConnection con = new SqlConnection(ConString)) { con.Open(); SqlCommand cmd = new SqlCommand(text, con); cmd.CommandType = type; obj = cmd.ExecuteScalar(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示"); } return obj; } /// <summary> /// 重载(返回数据集合第一行第一列的值) /// </summary> /// <param name="text"></param> /// <param name="para"></param> /// <param name="type"></param> /// <returns></returns> public static object ExcuteScalar(string text, SqlParameter[] para, CommandType type) { object obj = null; try { using (SqlConnection con = new SqlConnection(ConString)) { con.Open(); SqlCommand cmd = new SqlCommand(text, con); cmd.CommandType = type; cmd.Parameters.AddRange(para); obj = cmd.ExecuteScalar(); } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示"); } return obj; } #endregion } }
时间: 2024-11-05 19:00:54