1.在App.config中的<configuration></configuration>加入连接串 例: 第一种方法
<connectionStrings> <add name="con" connectionString="server=.;database=数据库名称;uid=sa;pwd=sa"/> </connectionStrings>
列:第二种方法的连接串直接在DBHelper类中写入连接串
private readonly string sqlcon = "server=.;database=数据库名称;uid=sa;pwd=sa;";
2.在DBHelper中写入
//第一种 private static SqlConnection Con { get { var con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString); con.Open(); return con; } } //第二种 都可以用 public SqlConnection sqlCon() { SqlConnection sql = new SqlConnection(sqlcon); sql.Open(); return sql; } private static SqlCommand Cmd { get { return new SqlCommand { Connection = Con }; } } /// <summary> /// 增删改 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static bool Update(string sql) { var cmd = Cmd; cmd.CommandText = sql; try { return cmd.ExecuteNonQuery() > 0; } finally { cmd.Connection.Close(); } } /// <summary> /// 获取一个对象 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static object SelectForScalar(string sql) { var cmd = Cmd; cmd.CommandText = sql; try { return cmd.ExecuteScalar(); } finally { cmd.Connection.Close(); } } /// <summary> /// 获取一个结果集 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static SqlDataReader SelectForReader(string sql) { var cmd = Cmd; cmd.CommandText = sql; try { return cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { cmd.Connection.Close(); throw; } }
以上就是把dbhelper帮助工具类写好了
3.例如对数据库某个表进行增删改查的话 例:对一个Class班级表进行操作演示
3.1先创建一个对应数据库的实体表
/// <summary> /// 编号 /// </summary> public int Id { get; set; } /// <summary> /// 班级名称 /// </summary> public string ClassName { get; set; }
3.2写这个表对应的增删改查
/// <summary> /// 添加班级 /// </summary> /// <param name="cls"></param> /// <returns></returns> public bool CreateClass(Models.Class cls) { return DBHelper.Update($"insert into Classes values('{cls.ClassName}','false',getdate())"); } /// <summary> /// 修改班级 /// </summary> /// <param name="cls"></param> /// <returns></returns> public bool EditClass(Models.Class cls) { return DBHelper.Update($"update Classes set Name='{cls.ClassName}' where Id='{cls.Id}'"); } /// <summary> /// 删除班级 /// </summary> /// <param name="cls"></param> /// <returns></returns> public bool RemoveClass(Models.Class cls) { return DBHelper.Update($"update Classes set IsRemove='true' where Id='{cls.Id}'"); } /// <summary> /// 获取一个班级信息 /// </summary> /// <returns></returns> public Models.Class GetOneClasses(string className) { var dr = DBHelper.SelectForReader($"select Id,Name from Classes where Name='{className}'"); Models.Class classes = null; if (dr.Read()) { classes=new Models.Class() { Id = dr.GetInt32(0), ClassName = dr.GetString(1), }; } dr.Close(); return classes; } /// <summary> /// 获取所有班级信息 /// </summary> /// <returns></returns> public List<Models.Class> GetAllClasses() { var dr = DBHelper.SelectForReader("select Id,Name from Classes"); List<Models.Class> classes = new List<Models.Class>(); while (dr.Read()) { classes.Add(new Models.Class() { Id = dr.GetInt32(0), ClassName=dr.GetString(1), }); } dr.Close(); return classes; }
4.调用写好的增删改班级的语句
ClassService classSvc = new ClassService(); //这是你写Class增删改的类 实例化过来 /// <summary> /// 添加班级 /// </summary> /// <param name="name"></param> /// <param name="bornDate"></param> /// <param name="classId"></param> /// <returns></returns> public bool CreateClass(string name) { if (string.IsNullOrEmpty(name)) { throw new Exception("请输入班级姓名!"); } return classSvc.CreateClass(new Models.Class() { ClassName = name }); }
4.控制台直接调
//把3.类初始化到控制台 //列 Classes cls =new Classes() cls.CreateClass("一年级"); //OK了