码迷,mamicode.com
首页 > 其他好文 > 详细

重要!!!实体类、数据访问类

时间:2016-12-03 23:57:56      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:use   实体类   string   for   select   数据库表   datetime   user   date   

创建两个类:

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();
        }
    }
}
技术分享

重要!!!实体类、数据访问类

标签:use   实体类   string   for   select   数据库表   datetime   user   date   

原文地址:http://www.cnblogs.com/baimangguo/p/6129752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!