• ADO,NET 实体类 和 数据访问类


    啥也不说,看代码。

    --SQl中
    --建立ren的数据库,插入一条信息
    create database ren 
    go
    use ren
    go
    create table xinxi
    (
    code nvarchar(20) primary key,--编号
    name nvarchar(20)--名字
    )
    insert into xinxi values('1001','zhangsan')

    1、建立实体类:

    实体类:封装
    封装一个类,类名与数据库表名一致
    成员变量名与列名一致,多一个下划线
    成员变量封装完的属性,就会与数据表中的列名一致

    每一行数据都可以存成一个对象,操作这个对象,就相当于对某一行数据进行整体操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication71.App_Code
    {
        public class xinxi
        {
            private string _code;
            public string code
            {
                get { return _code; }
                set { _code = value; }
            }
            private string _name;
            public string name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    }

    2、建立数据访问类:

    就是将对数据库的一些操作,单独写到一个类中,封成一些方法,等待调用。

    结构看起来会非常清晰。

    就像html中的CSS样式表和javascript的关系

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.SqlClient;
    
    namespace ConsoleApplication71.App_Code
    {
    
        
    
        public class xinxidata
        {
            SqlConnection cnn = null;
            SqlCommand cmd = null;
    
            public xinxidata()
            {
                cnn = new SqlConnection("server=.;database=ren;user=sa;pwd=123");
                cmd = cnn.CreateCommand();
            }
    
    
            /// <summary>
            /// 增加
            /// </summary>
            /// <param name="x">信息对象</param>
            public void insert(xinxi x)
            {
                
                cmd.CommandText = "insert into xinxi values(@a,@b)";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@a",x.code);
                cmd.Parameters.Add("@b",x.name);
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();       
            }
    
            //查询
            public xinxi chaxun(string code)
            {
                xinxi xin = null;
                cmd.CommandText = "select * from xinxi where code=@code";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@code",code);
                cnn.Open();
                SqlDataReader ss = cmd.ExecuteReader();
                if (ss.HasRows)//判断有没有
                {
                    xin = new xinxi();
                    ss.Read();
                    xin.code=ss["code"].ToString();
                    xin.name=ss["name"].ToString();
                }
                cnn.Close();
                return xin;        
            }
    
            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="name"></param>
            public bool delete(string code)
            {
                bool b = false;
                cmd.CommandText = "delete from xinxi where code=@code";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@code",code);
                cnn.Open();
                try//看看有没有出错,即删没删成功
                {
                    cmd.ExecuteNonQuery();
                    b=true;
                }
                catch
                {
                    b=false;
                }
                cnn.Close();
                return b;
            }        
            public void quanbu()//查询全部信息
            {
                cmd.CommandText = "select * from xinxi";
                cnn.Open();
                SqlDataReader ss = cmd.ExecuteReader();
                while (ss.Read())
                {
                    Console.WriteLine(ss[0]+"  "+ss[1]);
                }
                cnn.Close();
            }
            public bool update(string code,string name)
            {
                bool b = false;
                cmd.CommandText = "update xinxi set name=@name where code=@code";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@name",name);
                cmd.Parameters.Add("@code",code);
                cnn.Open();
                try
                {
                    cmd.ExecuteNonQuery();
                    b = true;
                }
                catch { }
                cnn.Close();
                return b;
            }
        }
    }

     3,在main函数里面,增删改查

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ConsoleApplication71.App_Code;
    
    namespace ConsoleApplication71
    {
        class Program
        {
            static void Main(string[] args)
            {
                xinxidata sj = new xinxidata();//实例化
    #region  添加
                //xinxi xin = new xinxi();
                //Console.Write("请输入添加的编号:");
                //xin.code = Console.ReadLine();
                //Console.Write("请输入添加的名字:");
                //xin.name = Console.ReadLine();      
                //sj.insert(xin);
    #endregion
    
    #region  删除
                //Console.Write("请输入被删除编号");
                //string code = Console.ReadLine();
                //if (sj.chaxun(code) != null)//判断是否有这个编号
                //{
                //  bool T=  sj.delete(code);
                //  if (T == true)//如果删除成功
                //  {
                //      Console.WriteLine("删除成功");
                //  }
                //  else
                //  {
                //      Console.WriteLine("删除不成功");
                //  }
                //}
                //else
                //{
                //    Console.WriteLine("输入有误!!");
                //}
                #endregion
    #region 查询全部信息
                //sj.quanbu();
                #endregion
    
                #region 修改
                Console.Write("请输入要修改的编号:");
                string code = Console.ReadLine();
                if (sj.chaxun(code) != null)
                {
                    Console.Write("请输入要修改的名字:");
                    string name = Console.ReadLine();              
                    bool M= sj.update(code,name);
                   if (M == true)
                   {
                       Console.WriteLine("修改成功!");
                   }
                   else
                   {
                       Console.WriteLine("删除不成功");
                   }
    
                }
                else
                {
                    Console.WriteLine("输入有误,不存在此编号!!");
                }
                #endregion
                Console.ReadLine();
            }
        
        }
    }

    完!!

  • 相关阅读:
    SqlServer触发器的创建与使用
    SqlServer存储过程的创建与使用
    SqlServer视图的创建与使用
    U盘重装系统:手把手教你怎么使用U盘重装系统、清除登录密码
    附034.Kubernetes_v1.21.0高可用部署架构二
    附032.Kubernetes实现蓝绿发布
    CKS考试心得分享
    001.IT运维面试问题-Linux基础
    附031.Kubernetes_v1.20.4高可用部署架构二
    深入Netty逻辑架构,从Reactor线程模型开始
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/5870358.html
Copyright © 2020-2023  润新知