数据访问

对应命名空间:System.Data.SqlClient;

SqlConnection:连接对象
SqlCommand:命令对象
SqlDataReader:读取器对象

CommandText:命令文本

增、删、改、查

1.造连接字符串
string connstring = "server=.;database=mydb;user=sa;pwd=123";

          connstring:造的字符串名

          server指服务器一般是IP地址本机可以使用点;
          database指数据库名称要访问的数据库名称
          user数据库的用户名:一般是sa
          pwd数据库的密码:自己设置的

2.造连接对象
SqlConnection conn = new SqlConnection(connstring);

          conn:造的连接对象名

3.创建命令对象
SqlCommand cmd = conn.CreateCommand();

          cmd:造的命令对象名

4.写要执行的SQL语句

  4-1:查询

  cmd.CommandText = "select * from Info";

  //4-2:添加

  cmd.CommandText = "Insert into Info values('p032','毒哥','True','n001','1987-02-02')";

  //4-3:删除

  cmd.CommandText = "delete from Info where Code='p032';

  //4-4:更改

   cmd.CommandText = "update Info set name='情方方' where Code='p032';

【打开连接】
conn.Open();  //可放在执行之前的任意位置

5.执行操作

  5-1:(读取操作,返回读取器对象)

  SqlDataReader dr = cmd.ExecuteReader();

  //5-2.执行操作(增删改操作,返回行数)
  cmd.ExecuteNonQuery();

6.处理数据

  6-1:查询一条数据

  if (dr.HasRows)      //HasRows 判断是否有行数据 bool型,返回true/false
  {
    dr.Read();    //dr.Read() 是数据库数据访问指针,每执行一次都会向下走一行,如果有内容则返回true,同时dr访问为当前行数据集合,

               可以使用索引或是列名来访问相对应的数据

    Console.WriteLine(dr[0]);
    Console.ReadLine();
  }
  else
  {
    Console.WriteLine("读取失败!");
  }

  //6-2.查询多条数据

  if (dr.HasRows)
  {
    while(dr.Read())  //使用while循环读取所有数据  一行数据是一个数组,一行数据里有多少列就有多少个索引
  {
    Console.WriteLine(dr[0]+"----"+dr[1]);
  }

    Console.ReadLine();
  }
  else
  {
    Console.WriteLine("没有读到数据");
    Console.ReadLine();
  }

【关闭连接】

conn.Close();

例:根据用户输入一个条件查询数据

        static void Main1(string[] args)
        {
            //用户输入内容
            Console.WriteLine("请输入要查询的名称:");
            string str = Console.ReadLine();

            //造连接字符串
            string connstring = "server=.;database=mydb;user=sa;pwd=123";

            //造连接对象
            SqlConnection conn = new SqlConnection(connstring);

            //造命令对象
            SqlCommand cmd = conn.CreateCommand();

            //准备一条SQL语句
            cmd.CommandText = "select * from Info where Name like '%"+str+"%'";

            //打开连接
            conn.Open();

            //执行SQL语句
            SqlDataReader dr = cmd.ExecuteReader();

            //读取数据
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    ;
                    while ( n <dr.FieldCount ) //.FieldCount获取当前行的列数
                    {
                        Console.Write(dr[n]+"\t");
                        n++;
                    }
                    Console.WriteLine();
                }
            }
            else
            {
                Console.WriteLine("没有查到满足条件的数据");
            }

            //关闭连接
            conn.Close();

            Console.ReadLine();

        }

例:让用户输入要删除的数据主键值(此方法不安全)

 static void Main4(string[] args)
        {
            //用户输入要删除的数据主键值
            Console.WriteLine("请输入要删除的代号:");
            string code = Console.ReadLine();

            //判断该数据存不存在
            SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from Info where Code='"+code+"'";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows)
            {
                //说明该数据存在
                Console.WriteLine("查到该数据,是否要执行删除操作,如果要删除请输入:1");
                int sc = Convert.ToInt32(Console.ReadLine());

                )
                {
                    //删除
                    dr.Close(); //关闭读取器

                    cmd.CommandText = "delete from Info where Code='"+code+"'";
                    cmd.ExecuteNonQuery();
                    Console.WriteLine("删除成功!");

                }
                else
                {
                    //不删除
                    dr.Read();

                    ])?"男":"女";
                    ].ToString());

                    ]+]+];

                    Console.WriteLine(str);

                }
            }
            else
            {
                //数据不存在
                Console.WriteLine("输入的代号错误!");
            }

            conn.Close();
            Console.ReadLine();
        }

        static string MinZu(string code)
        {
            string name="";
            SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select Name from Nation where Code = '" + code + "'";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                name = dr[].ToString();
            }
            conn.Close();

            return name;
        }

例:让用户输入要添加的内容

        static void Main3(string[] args)
        {
            //让用户输入要添加的内容
            Console.WriteLine("请输入要添加的代号:");
            string code = Console.ReadLine();

            Console.WriteLine("请输入姓名:");
            string name = Console.ReadLine();

            Console.WriteLine("请输入性别:");
            bool sex = Console.ReadLine()=="男"?true:false;

            Console.WriteLine("请输入民族:");
            string nation = Console.ReadLine();

            Console.WriteLine("请输入生日:");
            string birthday = Console.ReadLine();

            string nationcode = "n001";

            //将民族名称转为名族代号
            SqlConnection conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select Code from Nation where Name = '"+nation+"'";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                dr.Read();
                nationcode = dr[].ToString();
            }
            conn.Close();

            //往Info表添加数据
            cmd.CommandText = "insert into Info values('"+code+"','"+name+"','"+sex+"','"+nationcode+"','"+birthday+"')";
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            Console.WriteLine("添加成功!");

            Console.ReadLine();
        }

2016年11月28日--ADO.Net 增、删、改、查的更多相关文章

  1. 2016年11月28日--ADO.Net 查、插、删、改 小练习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 2016年11月28日 星期一 --出埃及记 Exodus 20:19

    2016年11月28日 星期一 --出埃及记 Exodus 20:19 and said to Moses, "Speak to us yourself and we will listen ...

  3. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  4. 2016年11月14日--SQL创建数据库、表-查、插、删、改

    --创建数据库(create database 数据库名)create database hq20161114go --使用选择数据库(use 数据库名)use hq20161114go --创建学生 ...

  5. 2016年12月28日 星期三 --出埃及记 Exodus 21:23

    2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...

  6. 2016年11月30日 星期三 --出埃及记 Exodus 20:21

    2016年11月30日 星期三 --出埃及记 Exodus 20:21 The people remained at a distance, while Moses approached the th ...

  7. 2016年11月29日 星期二 --出埃及记 Exodus 20:20

    2016年11月29日 星期二 --出埃及记 Exodus 20:20 Moses said to the people, "Do not be afraid. God has come t ...

  8. 2016年11月27日 星期日 --出埃及记 Exodus 20:18

    2016年11月27日 星期日 --出埃及记 Exodus 20:18 When the people saw the thunder and lightning and heard the trum ...

  9. 2016年11月26日 星期六 --出埃及记 Exodus 20:17

    2016年11月26日 星期六 --出埃及记 Exodus 20:17 "You shall not covet your neighbor's house. You shall not c ...

随机推荐

  1. mybatis缓存

    mybatis缓存http://www.cnblogs.com/QQParadise/articles/5109633.htmlhttp://www.mamicode.com/info-detail- ...

  2. C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 服务器之间的接口通讯功、信息交换

    1:当远程调用方法时,会有很多种可能性发生.接口调用之后,发生错误是什么原因发生的?反馈给开发人员需要精确.精准.高效率,这时候若能返回出错状态信息的详细信息,接口之间的调用就会非常顺利,各种复杂问题 ...

  3. window.open和window.location.href的几种用法

    windows.open("URL","窗口名称","窗口外观设定"); <A href="javascript:windo ...

  4. Win10 UWP开发实现Bing翻译

    微软在WP上的发展从原来的Win7到Win8,Win8.1,到现在的Win10 UWP,什么是UWP,UWP即Windows 10 中的Universal Windows Platform简称.即Wi ...

  5. airline 設定 安裝

    .vimrc " install airline plugin using Vundle Plugin 'vim-airline/vim-airline' " install ai ...

  6. Oracle11g字符集AL32UTF8修改为ZHS16GBK详解【转】

    ------感谢作者,确实解决了问题.分享下,希望帮到更多人 此问题发生在数据库迁移过程中.源数据库:自己笔记本上win7 64位系统的oracle11g个人版,字符集ZHS16GBK :目标数据库, ...

  7. 软件开发学习笔记 <二>软件开发模型、Up、Rup、敏捷Up

    软件开发过程(process) 是一个将用户需求转化为软件系统所需要的活动的集合. 软件生命周期(SDLC,Software Devlopment Life Cycle) 软件从孕育.诞生.成长.成熟 ...

  8. TCP协议中的三次握手和四次挥手(图解)

    建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接后回复ACK报文,并为这次连接分配资 ...

  9. .net 执行sql包含go语句的处理

    今天工作中遇到了这个问题,其实只要把GO替换成“;”就行了,其它人写的例子用Split来拆分这一个脚本文件的内容,完全没有必要

  10. TYVJ1338 QQ农场

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 Sandytea前段时间沉迷于QQ农场中……一天夜里,他梦见来到好友X的农场上…… 描述 这个农场和游戏中略有 ...