using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data;
namespace SQLHelper
{
public class Class1
{
static string strcoon = System.Configuration.ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
-- #region 封装ExcuteNonQuery
代码 public static int ExcuteNonQurey(string commandText, params SqlParameter[] para) { using (SqlConnection conn = new SqlConnection(strcoon)) { using (SqlCommand cmd = new SqlCommand(commandText, conn)) { if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } if (para != null && para.Length != 0) { cmd.Parameters.AddRange(para); } return cmd.ExecuteNonQuery(); } } } #endregion
-- #region 二次封装ExcuteNonQuery
代码 public static int ExcuteNonQurey(string commandText, params object[] objs) { MatchCollection ms = Regex.Matches(commandText, @"@\w+"); List<SqlParameter> list = new List<SqlParameter>(); if (ms.Count != objs.Length) { throw new Exception(); } for (int i = 0; i < ms.Count; i++) { list.Add(new SqlParameter(ms[i].Value, objs[i])); } return ExcuteNonQurey(commandText, list.ToArray()); } #endregion
-- #region 封装ExcuteScalar
代码 public static object ExcuteScalar(string commandText, params SqlParameter[] para) { using (SqlConnection conn = new SqlConnection(strcoon)) { using (SqlCommand cmd = new SqlCommand(commandText, conn)) { if (para != null && para.Length != 0) { cmd.Parameters.AddRange(para); } if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } return cmd.ExecuteScalar(); } } } #endregion
-- #region 二次封装ExcuteScalar
代码 public static object ExcuteScalar(string commandText, params object[] objs) { MatchCollection ms = Regex.Matches(commandText, @"@\w+"); List<SqlParameter> list = new List<SqlParameter>(); if (ms.Count != objs.Length) { throw new Exception(); } for (int i = 0; i < ms.Count; i++) { list.Add(new SqlParameter(ms[i].Value, objs[i])); } return ExcuteScalar(commandText, list.ToArray()); } #endregion
-- #region 封装ExcuteReader
代码 public static SqlDataReader ExcuteReader(string commandText, params SqlParameter[] para) { SqlConnection conn = new SqlConnection(strcoon); using (SqlCommand cmd = new SqlCommand(commandText, conn)) { if (para != null && para.Length != 0) { cmd.Parameters.AddRange(para); } if (conn.State == System.Data.ConnectionState.Closed) { conn.Open(); } return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection); } } #endregion
-- #region 二次封装ExcuteReader
代码public static SqlDataReader ExcuteReader(string commandText, params object[] objs) { MatchCollection ms = Regex.Matches(commandText, @"@\w+"); List<SqlParameter> list = new List<SqlParameter>(); if (ms.Count != objs.Length) { throw new Exception(); } for (int i = 0; i < ms.Count; i++) { list.Add(new SqlParameter(ms[i].Value, objs[i])); } return ExcuteReader(commandText, list.ToArray()); } #endregion
-- #region 封装DataAdapter
代码 public static DataSet DataAdapter(string commandText, params SqlParameter[] para) { DataSet ds = new DataSet(); using (SqlDataAdapter sda = new SqlDataAdapter(commandText, strcoon)) { if (para != null && para.Length != 0) { sda.SelectCommand.Parameters.AddRange(para); } sda.Fill(ds); return ds; } } #endregion
-- #region 二次封装DataAdapter
代码 public static DataSet DataAdapter(string commandText, params object[] objs) { MatchCollection ms = Regex.Matches(commandText, @"@\w+"); List<SqlParameter> list = new List<SqlParameter>(); if (ms.Count != objs.Length) { throw new Exception(); } for (int i = 0; i < ms.Count; i++) { list.Add(new SqlParameter(ms[i].Value, objs[i])); } return DataAdapter(commandText, list.ToArray()); } #endregion }