国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#數(shù)據(jù)庫(kù)操作小結(jié)

2020-01-24 03:41:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
1、常用的T-Sql語(yǔ)句
      查詢:SELECT * FROM tb_test WHERE ID='1' AND name='xia'
                SELECT * FROM tb_test
      插入:INSERT INTO tb_test VALUES('xia','123')
                  INSERT INTO tb_test(name) VALUES('xia')
      更新:UPDATE tb_test SET password='234' WHERE ID='1'
      刪除:DELETE FROM tb_test WHERE ID='1'
                 DELETE tb_test WHERE ID='1'
2、在vs2010中獲取數(shù)據(jù)庫(kù)連接字符串
      string connectionString = Properties.Settings.Default.DatabaseTestConnectionString;
3、SqlCommand類型
       查詢:
      
復(fù)制代碼 代碼如下:

       using (SqlConnection connection = new SqlConnection(connectionString))
       {
             try
             {
                    SqlCommand command = new SqlCommand(selectStr, connection);
                    command.Connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                     while (reader.Read())
                             label1.Text = "name:" + reader["name"].ToString();    //數(shù)據(jù)讀取
                     command.Connection.Close();
               }
              catch (SqlException ex)
              {
                    throw ex;
              }
       }
      

       插入、修改、刪除:
      
復(fù)制代碼 代碼如下:

       using (SqlConnection connection = new SqlConnection(connectionString))
       {
             try
             {
                    SqlCommand command = new SqlCommand(cmdStr, connection);
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    command.Connection.Close();
              }
              catch (SqlException ex)
              {
                    throw ex;
              }
      }

4、DataTable類型,查詢、添加、修改、刪除
      DataTable使用查詢、添加、刪除、修改時(shí),需要用到SqlDataAdapter類
      string selectStr = "SELECT * FROM tb_test2";
      查詢:
     
復(fù)制代碼 代碼如下:

      using (SqlConnection connection = new SqlConnection(connectionString))
      {
             try
             {
                    SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
                    DataTable dataTable = new DataTable();
                    adapter.Fill(dataTable);
                    //數(shù)據(jù)讀取
                    label1.Text = dataTable.Rows[0][0].ToString();
              }
             catch (SqlException ex)
             {
                     throw ex;
              }
      }
     

      添加:
     
復(fù)制代碼 代碼如下:

      using (SqlConnection connection = new SqlConnection(connectionString))
      {
           try
           {
                 SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
                 DataTable dataTable = new DataTable();
                 adapter.Fill(dataTable);
                  //添加數(shù)據(jù)
                 DataRow newRow = dataTable.NewRow();
                 newRow["id"] = "tesr";
                 newRow["name"] = "111";
                 dataTable.Rows.Add(newRow);
                 SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                 adapter.Update(dataTable); //更新到數(shù)據(jù)庫(kù)
            }
            catch (SqlException ex)
            {
                 throw ex;
            }
      }
     

      修改:
     
復(fù)制代碼 代碼如下:

      using (SqlConnection connection = new SqlConnection(connectionString))
      {
            try
            {
                  SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
                  DataTable dataTable = new DataTable();
                  adapter.Fill(dataTable);
                  //修改數(shù)據(jù)
                 DataRow updateRow = dataTable.Rows[0];
                 updateRow["id"] = "update";
                 updateRow["name"] = "222";
                 SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                 adapter.Update(dataTable); //更新到數(shù)據(jù)庫(kù)
            }
           catch (SqlException ex)
           {
                 throw ex;
           }
      }
     

      刪除:
     
復(fù)制代碼 代碼如下:

      using (SqlConnection connection = new SqlConnection(connectionString))
      {
            try
            {
                   SqlDataAdapter adapter = new SqlDataAdapter(selectStr, connection);
                   DataTable dataTable = new DataTable();
                   adapter.Fill(dataTable);
                   dataTable.Rows[0].Delete(); //刪除記錄
                   SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
                   adapter.Update(dataTable); //更新到數(shù)據(jù)庫(kù)
             }
            catch (SqlException ex)
            {
                   throw ex;
            }
      }

5、DataSet類型
      DataSet操作跟DataTabel操作基本是一樣的,只是DataSet可以儲(chǔ)存有多個(gè)表格,所以就多做介紹了
6、個(gè)人總結(jié)
      個(gè)人感覺(jué),用 SqlCommand比較靈活,而DataSet是實(shí)現(xiàn)ADO.NET斷開(kāi)式連接的核心,比較安全
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 深州市| 浙江省| 石柱| 加查县| 赣榆县| 马龙县| 桐梓县| 三门县| 科尔| 姜堰市| 凤庆县| 古交市| 出国| 赞皇县| 泾川县| 瓮安县| 三门峡市| 柘荣县| 潞西市| 东明县| 临夏县| 仙游县| 吉木乃县| 白水县| 米林县| 巴彦淖尔市| 宜黄县| 凤台县| 米易县| 宾川县| 郧西县| 新巴尔虎左旗| 辉县市| 桐梓县| 福州市| 中西区| 贵溪市| 唐山市| 庆元县| 无棣县| 八宿县|