复习了在学校的时候做的WinForm端的一个学生信息管理系统,用的三层架构,看了一下里面的数据优化类
这个类是用来把对数据库的操作封装成静态方法,增删改查的时候直接调用这个类,减少项目里代码的冗余和方便以后修改。
Dbhelper类代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using System.Data; using Model; namespace DAL { class DBHelper { #region 获得连接对象 Connsql public static SqlConnection Connsql() { string sql = "server=.;database=vs;integrated security=sspi"; SqlConnection conn = new SqlConnection(sql); return conn; } //执行 增加,删除,修改的sql语句 #endregion #region 增加删除修改的sql语句执行方法 ExecuteNonQuery public static int ExecuteNonQuery(string sql,CommandType type,params SqlParameter[] sp) { SqlConnection conn = DBHelper.Connsql(); conn.Open(); SqlCommand sc = new SqlCommand(sql, conn); sc.CommandType = type; if(sp!=null) { foreach (SqlParameter p in sp) { sc.Parameters.Add(p); } } int i = sc.ExecuteNonQuery(); conn.Close(); return i; } #endregion #region 数据库返回一个值时的sql语句执行方法 public static Object ExecuteScalar(string sql,CommandType type, params SqlParameter[] paras) { SqlConnection conn = DBHelper.Connsql(); conn.Open(); SqlCommand sc = new SqlCommand(sql,conn); sc.CommandType=type; if(paras!=null) { foreach(SqlParameter s in paras ) { sc.Parameters.Add(s); } } object result = sc.ExecuteScalar(); conn.Close(); return result; } #endregion #region 数据库返回多行记录的执行方法 public static SqlDataReader ExecuteDataReader(string sql,CommandType type,params SqlParameter[] paras) { SqlConnection conn = DBHelper.Connsql(); conn.Open(); SqlCommand sc = new SqlCommand(sql,conn); sc.CommandType = type; if (paras != null) { foreach (SqlParameter s in paras) { sc.Parameters.Add(s); } } SqlDataReader dr = sc.ExecuteReader(CommandBehavior.CloseConnection); return dr; } #endregion #region 返回一个数据集时执行方法 public static DataSet ExecuteDataSet(string sql,CommandType type,params SqlParameter [] paras) { SqlConnection conn = DBHelper.Connsql(); conn.Open(); SqlCommand sc = new SqlCommand(sql,conn); sc.CommandType = type; if (paras != null) { foreach(SqlParameter s in paras) { sc.Parameters.Add(s); } } SqlDataAdapter sda = new SqlDataAdapter(sc); DataSet ds = new DataSet(); sda.Fill(ds); return ds; } #endregion } }
调用DbHelper类:
1.返回一个值:
int result =(int)this.ExecuteScalar("select count(*) from UserAccount", CommandType.Text);
2.返回一个数据集(存放到Dataset):
DataSet ds = ExecuteDataSet("select * from UserAccount", CommandType.Text); return ds.tables[0];
3.返回一个数据集(存放到List),性能上比DataSet要好
#region 将数据填充到List集合中 public List<tbSpecialty> GettbSpecialty() { List<tbSpecialty> sp = new List<tbSpecialty>(); SqlDataReader dr = DBHelper.ExecuteDataReader("select * from tbSpecialtyInfo", CommandType.Text); while(dr.Read()) { tbSpecialty tsp = new tbSpecialty(); tsp.SpecialtyName = dr["SpecialtyName"].ToString(); sp.Add(tsp); } dr.Close(); return sp; } #endregion
4.增删改:
#region public int AddtbSpecialty(tbSpecialty sp) { string sql = "insert into tbSpecialtyInfo values(@name,@remark)"; SqlParameter p1 = new SqlParameter("@name", sp.SpecialtyName); SqlParameter p2 = new SqlParameter("@remark",sp.Remark); int result = DBHelper.ExecuteNonQuery(sql, CommandType.Text, p1, p2); return result; } #endregion
时间: 2024-11-09 03:25:53