最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2012年09月24日 20:14:11   作者:  
每次做項(xiàng)目都會用到數(shù)據(jù)庫,對數(shù)據(jù)庫的操作都是糊里糊涂從書里找代碼用。通過昨天晚上與今天早上的努力,把數(shù)據(jù)庫的操作整理了一下,下面把整理結(jié)果做個(gè)小結(jié)
1、常用的T-Sql語句
      查詢: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ù)庫連接字符串
      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ù)庫
            }
            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ù)庫
            }
           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ù)庫
             }
            catch (SqlException ex)
            {
                   throw ex;
            }
      }

5、DataSet類型
      DataSet操作跟DataTabel操作基本是一樣的,只是DataSet可以儲存有多個(gè)表格,所以就多做介紹了
6、個(gè)人總結(jié)
      個(gè)人感覺,用 SqlCommand比較靈活,而DataSet是實(shí)現(xiàn)ADO.NET斷開式連接的核心,比較安全
  • C#中static void Main(string[] args) 參數(shù)示例詳解

    C#中static void Main(string[] args) 參數(shù)示例詳解

    這篇文章主要介紹了C#中static void Main(string[] args) 參數(shù)詳解,本文通過具體示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2017-03-03
  • C#使用OpenCvSharp實(shí)現(xiàn)圖像校正

    C#使用OpenCvSharp實(shí)現(xiàn)圖像校正

    這篇文章主要為大家詳細(xì)介紹了C#如何使用OpenCvSharp實(shí)現(xiàn)圖像校正功能,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價(jià)值,需要的小伙伴可以參考下
    2023-11-11
  • C#實(shí)現(xiàn)單線程異步互斥鎖的示例代碼

    C#實(shí)現(xiàn)單線程異步互斥鎖的示例代碼

    異步互斥鎖的作用是用于確保存在異步操作的上下文同步互斥,這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)單線程異步互斥鎖,文中的示例代碼講解詳細(xì),需要的可以參考下
    2024-01-01
  • C#窗體間通訊處理的幾種方法總結(jié)

    C#窗體間通訊處理的幾種方法總結(jié)

    這篇文章主要介紹了
    2013-11-11
  • C#中ListView用法實(shí)例

    C#中ListView用法實(shí)例

    我們經(jīng)常會在應(yīng)用程序中使用列表的形式來展現(xiàn)一些內(nèi)容,所以學(xué)好ListView是非常必需的,下面這篇文章主要給大家介紹了關(guān)于C#中ListView用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹的方法

    C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹的方法

    這篇文章主要介紹了C#使用前序遍歷、中序遍歷和后序遍歷打印二叉樹的方法,涉及C#遍歷二叉樹的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • C#獲取真實(shí)IP地址實(shí)現(xiàn)方法

    C#獲取真實(shí)IP地址實(shí)現(xiàn)方法

    這篇文章主要介紹了C#獲取真實(shí)IP地址實(shí)現(xiàn)方法,對比了C#獲取IP地址的常用方法并實(shí)例展示了C#獲取真實(shí)IP地址的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C#獲取機(jī)器碼的方法詳解(機(jī)器名,CPU編號,硬盤編號,網(wǎng)卡mac等)

    C#獲取機(jī)器碼的方法詳解(機(jī)器名,CPU編號,硬盤編號,網(wǎng)卡mac等)

    這篇文章主要介紹了C#獲取機(jī)器碼的方法,結(jié)合實(shí)例形式詳細(xì)分析了C#獲取硬件機(jī)器名、CPU編號、硬盤編號、網(wǎng)卡mac等信息的相關(guān)實(shí)現(xiàn)方法,需要的朋友可以參考下
    2016-07-07
  • Unity C#執(zhí)行bat腳本的操作

    Unity C#執(zhí)行bat腳本的操作

    這篇文章主要介紹了Unity C#執(zhí)行bat腳本的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 最新評論

    留坝县| 清苑县| 晋中市| 定远县| 保德县| 平原县| 独山县| 巴彦县| 吴忠市| 潼南县| 神农架林区| 南皮县| 尉氏县| 新疆| 来凤县| 团风县| 恩平市| 北京市| 遂昌县| 定安县| 华池县| 永新县| 南雄市| 洛扎县| 肃宁县| 阳西县| 沙雅县| 固阳县| 营口市| 宁晋县| 通辽市| 加查县| 鄢陵县| 铜陵市| 扎鲁特旗| 安阳县| 江安县| 唐河县| 高碑店市| 无锡市| 潞城市|