实体类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 实体类_数据访问类.App_Code
{
public class Users
{
private int _code;
/// <summary>
/// code
/// </summary>
public int Code
{
get { return _code; }
set { _code = value; }
}
private string _usename;
/// <summary>
/// 用户名
/// </summary>
public string Usename
{
get { return _usename; }
set { _usename = value; }
}
private string _password;
/// <summary>
/// 用户密码
/// </summary>
public string Password
{
get { return _password; }
set { _password = value; }
}
private string _nick;
/// <summary>
/// 昵称
/// </summary>
public string Nick
{
get { return _nick; }
set { _nick = value; }
}
private bool _sex;
/// <summary>
/// 性别
/// </summary>
public bool Sex
{
get { return _sex; }
set { _sex = value; }
}
private string _nation;
/// <summary>
/// 民族
/// </summary>
public string Nation
{
get { return _nation; }
set { _nation = value; }
}
private string _class;
/// <summary>
/// 班级
/// </summary>
public string _class1
{
get { return _class; }
set { _class = value; }
}
private DateTime _birthday;
/// <summary>
/// 生日
/// </summary>
public DateTime Birthday
{
get { return _birthday; }
set { _birthday = value; }
}
}
}

数据访问类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient; namespace 实体类_数据访问类.App_Code
{
public class UserDate
{
SqlConnection conn = null;
SqlCommand com = null;
public UserDate()
{
conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");
com = conn.CreateCommand();
}
/// <summary>
/// 将数据添加到Users表中,添加成功返回true,失败返回false
/// </summary>
/// <param name="u">要添加到表中的Users对象</param>
/// <returns>添加成功返回true,失败返回false</returns>
public bool insert(Users u)
{
bool ok = false;
int count = ;
com.CommandText = "insert into Users values(@usename,@password,@nick,@sex,@nation,@class,@birthday)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@usename",u.Usename);
com.Parameters.AddWithValue("@password", u.Password);
com.Parameters.AddWithValue("@nick", u.Nick);
com.Parameters.AddWithValue("@sex", u.Sex);
com.Parameters.AddWithValue("@nation", u.Nation);
com.Parameters.AddWithValue("@class", u._class1);
com.Parameters.AddWithValue("@birthday", u.Birthday);
try
{
conn.Open();
count=com.ExecuteNonQuery();
}
catch
{
ok = false;
}
finally
{
conn.Close();
}
if(count>)
{
ok = true;
} return ok;
} /// <summary>
/// 通过泛型集合对数据进行查询
/// </summary>
/// <returns>返回一个集合</returns>
public List<Users> Select()
{
List<Users> list= new List<Users>();
com.CommandText = "select *from Users";
conn.Open();
SqlDataReader dr = com.ExecuteReader();
if(dr.HasRows)
{
while(dr.Read())
{
Users u = new Users();
u.Code = Convert.ToInt32(dr["code"]);
u.Usename = dr["usename"].ToString();
u.Password = dr["password"].ToString();
u.Nick = dr["nick"].ToString();
u.Sex = Convert.ToBoolean(dr["sex"]);
u.Nation = dr["nation"].ToString();
u._class1 = dr["class"].ToString();
u.Birthday = Convert.ToDateTime(dr["birthday"]); list.Add(u);
} } conn.Close();
return list;
} /// <summary>
/// 根据usename进行查询
/// </summary>
/// <param name="usename">要查询的usename对象</param>
/// <returns>返回bool类型的值,返回true表示有usename这条数据</returns>
public bool select(string usename)
{
bool has = false;
com.CommandText = "select *from Users where usename=@usename";
com.Parameters.Clear();
com.Parameters.AddWithValue("@usename",usename);
conn.Open();
SqlDataReader dr = com.ExecuteReader();
if(dr.HasRows)
{
has = true;
}
conn.Close();
return has;
}
//public void delete(string username)
//{
// com.CommandText = "delete from Users where usename=@usename";
// com.Parameters.Clear();
// com.Parameters.AddWithValue("@usename", username); // conn.Open();
// com.ExecuteNonQuery();
// conn.Close();
//} /// <summary>
/// 删除数据
/// </summary>
/// <param name="username">要查询的usename对象</param>
/// <returns>返回bool类型的值,返回true表示删除成功</returns>
public bool delete(string username)
{
bool have = false;
int cc = ;
com.CommandText = "delete from Users where usename=@usename";
com.Parameters.Clear();
com.Parameters.AddWithValue("@usename", username);
try
{
conn.Open();
cc = com.ExecuteNonQuery();
}
catch
{
have = false;
}
finally
{
conn.Close();
}
if (cc > )
{
have = true;
}
return have;
}
}
}

业务逻辑类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 实体类_数据访问类.App_Code; namespace 实体类_数据访问类
{
class Program
{
static void Main(string[] args)
{
//完整查询操作,创建泛型数组引用UsersDate的Select方法
List<Users> ulist = new UserDate().Select();
//遍历数组
foreach(Users uuu in ulist)
{
Console.WriteLine(uuu.Code+"\t"+uuu.Usename+"\t"+uuu.Password+"\t"+uuu.Nick+"\t"+uuu.Sex+"\t"+uuu.Nation+"\t"+uuu._class1+"\t"+uuu.Birthday);
} //完整删除操作
Console.WriteLine("请输入要删除的用户名:");
string uname = Console.ReadLine();
//select方法的返回值为true,说明有此用户
if (new UserDate().select(uname))
{
Console.WriteLine("已查到此用户信息,是否要删除?(Y/N)");//用户操作
string yn = Console.ReadLine();
if (yn.ToUpper() == "Y")
{
//delete方法的返回值为true,删除成功
if (new UserDate().delete(uname))
Console.WriteLine("删除成功!");
//delete方法的返回值为false,删除失败
else
Console.WriteLine("删除失败!");
} else
{
Console.WriteLine("取消删除!!");
}
}
//select方法的返回值为false,无此用户
else
{
Console.WriteLine("未查到此用户信息!!");
}
//Console.WriteLine("请输入要删除的用户名:");
//string uname = Console.ReadLine();
//if (new UserDate().select(uname))
//{
// Console.WriteLine("已查到此用户信息,是否要删除?(Y/N)");
// string yn = Console.ReadLine();
// if (yn.ToUpper() == "Y")
// {
// new UserDate().delete(uname);
// Console.WriteLine("删除成功!"); // }
// else
// {
// Console.WriteLine("取消删除!!");
// }
//}
//else
//{
// Console.WriteLine("未查到此用户信息!!");
//} //添加用户操作
Users user = new Users();
Console.WriteLine("请输入用户名:");
user.Usename = Console.ReadLine();
Console.WriteLine("请输入密码:");
user.Password = Console.ReadLine();
Console.WriteLine("请输入昵称:");
user.Nick = Console.ReadLine();
Console.WriteLine("请输入性别:");
user.Sex = Convert.ToBoolean(Console.ReadLine());
Console.WriteLine("请输入民族:");
user.Nation = Console.ReadLine();
Console.WriteLine("请输入班级:");
user._class1 = Console.ReadLine();
Console.WriteLine("请输入生日:");
user.Birthday = Convert.ToDateTime(Console.ReadLine()); UserDate ud = new UserDate();
bool isok= ud.insert(user);//数据访问类中insert方法的返回值
if (isok)
Console.WriteLine("添加成功!");
else
Console.WriteLine("添加失败!!");
Console.ReadLine();
}
}
}
04-28 13:19