ADO.NET 增刪改、查


數據訪問

對應命名空間: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())
                {
                    int n = 0;
                    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());

                if (sc == 1)
                {
                    //刪除
                    dr.Close(); //關閉讀取器

                    cmd.CommandText = "delete from Info where Code='"+code+"'";
                    cmd.ExecuteNonQuery();
                    Console.WriteLine("刪除成功!");
                    
                }
                else
                {
                    //不刪除
                    dr.Read();

                    string sex = Convert.ToBoolean(dr[2])?"":"";
                    string nation = MinZu(dr[3].ToString());

                    string str = "代號:"+dr[0]+"\t姓名:"+dr[1]+"\t性別:"+sex+"\t民族:"+nation+"\t生日:"+dr[4];

                    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[0].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[0].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();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM