重要!!!实体类数据访问类
Posted 不会撩妹的白芒果
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了重要!!!实体类数据访问类相关的知识,希望对你有一定的参考价值。
创建两个类:
users类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实体类_数据访问类.App_Code { public class Users { private int _Ids; /// <summary> /// ids /// </summary> public int Ids { get { return _Ids; } set { _Ids = value; } } private string _UserName; /// <summary> /// 用户名 /// </summary> public string UserName { get { return _UserName; } set { _UserName = value; } } private string _PassWord; /// <summary> /// 密码 /// </summary> public string PassWord { get { return _PassWord; } set { _PassWord = value; } } private string _NickName; /// <summary> /// 昵称 /// </summary> public string NickName { get { return _NickName; } set { _NickName = value; } } private bool _Sex; /// <summary> /// 性别 /// </summary> public bool Sex { get { return _Sex; } set { _Sex = value; } } private DateTime _Birthday; /// <summary> /// 生日 /// </summary> public DateTime Birthday { get { return _Birthday; } set { _Birthday = value; } } private string _Nation; /// <summary> /// 民族 /// </summary> public string Nation { get { return _Nation; } set { _Nation = value; } } private string _Class; /// <summary> /// 班级 /// </summary> public string Class { get { return _Class; } set { _Class = value; } } } }
usersdata:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; namespace 实体类_数据访问类.App_Code { public class UsersData { SqlConnection conn = null; SqlCommand cmd = null; public UsersData() { conn = new SqlConnection("server=.;database=Data0928;user=sa;pwd=123"); cmd = conn.CreateCommand(); } /// <summary> /// 将数据添加到Users表中,返回true说明添加成功 /// </summary> /// <param name="u">要添加到数据表中的Users对象</param> /// <returns></returns> public bool Insert(Users u) { bool ok = false; int count = 0; cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f,@g)"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", u.UserName); cmd.Parameters.AddWithValue("@b", u.PassWord); cmd.Parameters.AddWithValue("@c", u.NickName); cmd.Parameters.AddWithValue("@d", u.Sex); cmd.Parameters.AddWithValue("@e", u.Birthday); cmd.Parameters.AddWithValue("@f", u.Nation); cmd.Parameters.AddWithValue("@g", u.Class); try { conn.Open(); count = cmd.ExecuteNonQuery(); } catch { ok = false; } finally { conn.Close(); } if (count > 0) ok = true; return ok; } //查询数据库表中所有数据 public List<Users> Select() { List<Users> list = new List<Users>(); cmd.CommandText = "select *from Users"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Users u = new Users(); u.Ids = Convert.ToInt32(dr["ids"]); u.UserName = dr["UserName"].ToString(); u.PassWord = dr["PassWord"].ToString(); u.NickName = dr["NickName"].ToString(); u.Sex = Convert.ToBoolean(dr["Sex"]); u.Birthday = Convert.ToDateTime(dr["Birthday"]); u.Nation = dr["Nation"].ToString(); u.Class = dr["Class"].ToString(); list.Add(u); } } conn.Close(); return list; } //查询指定用户名的数据 public bool Select(string username) { bool ok = false; cmd.CommandText = "select *from Users where UserName = @a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a",username); Users u = new Users(); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { u.Ids = Convert.ToInt32(dr["ids"]); u.UserName = dr["UserName"].ToString(); u.PassWord = dr["PassWord"].ToString(); u.NickName = dr["NickName"].ToString(); u.Sex = Convert.ToBoolean(dr["Sex"]); u.Birthday = Convert.ToDateTime(dr["Birthday"]); u.Nation = dr["Nation"].ToString(); u.Class = dr["Class"].ToString(); ok = true; } } conn.Close(); return ok; } //删除数据 public void Delete(string uname) { cmd.CommandText = "delete from Users where UserName = @a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a",uname); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } }
main函数中调用方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using 实体类_数据访问类.App_Code; namespace 实体类_数据访问类 { class Program { static void Main(string[] args) { //list<users> ulist=new list<users>; //userdata da=new userdata(); //ulist=da.select(); List<Users> ulist = new UsersData().Select(); foreach (Users uuu in ulist) { Console.WriteLine(uuu.Ids + " " + uuu.UserName + " " + uuu.PassWord + " " + uuu.NickName + " " + uuu.Sex + " " + uuu.Birthday + " " + uuu.Nation + " " + uuu.Class); } Console.Write("请输入要删除的用户名:"); string uname = Console.ReadLine(); if (new UsersData().Select(uname)) { Console.WriteLine("已查到此用户信息,是否要删除?(Y/N)"); string yn = Console.ReadLine(); if (yn.ToUpper() == "Y") { new UsersData().Delete(uname); Console.WriteLine("删除成功!"); } } Users user = new Users(); Console.Write("请输入用户名:"); user.UserName = Console.ReadLine(); Console.Write("请输入密码:"); user.PassWord = Console.ReadLine(); Console.Write("请输入昵称:"); user.NickName = Console.ReadLine(); Console.Write("请输入性别:"); user.Sex = Convert.ToBoolean(Console.ReadLine()); Console.Write("请输入生日:"); user.Birthday = Convert.ToDateTime(Console.ReadLine()); Console.Write("请输入民族:"); user.Nation = Console.ReadLine(); Console.Write("请输入班级:"); user.Class = Console.ReadLine(); UsersData ud = new UsersData(); bool isok = ud.Insert(user); if (isok) Console.WriteLine("添加成功!"); else Console.WriteLine("添加失败!"); Console.ReadKey(); } } }
以上是关于重要!!!实体类数据访问类的主要内容,如果未能解决你的问题,请参考以下文章
使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段