自己写的一个sqlhelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration ; using System.Data; using System.Data.SqlClient ;
namespace ADOTest { class SQLHelper { private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; /// <summary> /// 增,删,改,返回多行结果 /// </summary> /// <param name="sql"></param> /// <param name="pms"></param> /// <returns></returns> public static int ExcuteNonQuery(string sql,params SqlParameter [] pms ) { using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms!=null) { cmd.Parameters.AddRange(pms); } con.Open(); return cmd.ExecuteNonQuery(); } } }
/// <summary> /// 增,删,改,返回一行一列的结果 /// </summary> /// <param name="sql"></param> /// <param name="pms"></param> /// <returns></returns> public static object ExcuteScalar(string sql, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms!=null) { cmd.Parameters.AddRange(pms); } con.Open(); return cmd.ExecuteScalar(); } } }
/// <summary> /// 执行返回SqlDataReader /// </summary> /// <param name="sql"></param> /// <param name="pms"></param> /// <returns></returns> public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection(conStr)) { using (SqlCommand cmd = new SqlCommand(sql, con)) { if (pms!=null) { cmd.Parameters.AddRange(pms);
} con.Open(); return cmd.ExecuteReader(CommandBehavior.CloseConnection); } } }
/// <summary> /// 返回一个DataTable /// </summary> /// <param name="sql"></param> /// <param name="pms"></param> /// <returns></returns> public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms) { using (SqlConnection con = new SqlConnection (conStr)) { using (SqlDataAdapter adapter = new SqlDataAdapter (sql,con)) { if (pms!=null) { adapter.SelectCommand.Parameters.AddRange(pms); } DataTable dt = new DataTable(); adapter.Fill(dt); return dt; } } }
} }