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


    创建两个类:

    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();
            }
        }
    }
    复制代码
  • 相关阅读:
    在Eclipse中对包进行增删改查
    数据库中包的使用
    自定义函数执行动态sql语句
    用SQL语句创建四个表并完成相关题目-10月18日更新
    GUID简介
    设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。
    Java基础-服务器的发送和接收
    java基础-多线程执行
    Java基础-多线程编程-1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
    Java基础-输入输出-3.编写BinIoDemo.java的Java应用程序,程序完成的功能是:完成1.doc文件的复制,复制以后的文件的名称为自己的学号姓名.doc。
  • 原文地址:https://www.cnblogs.com/baimangguo/p/6129752.html
Copyright © 2020-2023  润新知