• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            我希望你是我獨家記憶

            一段永遠封存的記憶,隨風而去
            posts - 263, comments - 31, trackbacks - 0, articles - 3
               :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

            C#SQL簡單操作學習

            Posted on 2009-08-05 16:23 Hero 閱讀(193) 評論(0)  編輯 收藏 引用 所屬分類: C#積累
              1 using System;
              2 using System.Collections.Generic;
              3 using System.Text;
              4 
              5 using System.Data;
              6 using System.Data.SqlClient;
              7 using System.IO;
              8 
              9 namespace SQL
             10 {
             11     class Program
             12     {
             13         static void Main1( string[] args )
             14         {
             15             //創建一個新連接字符串
             16             SqlConnection thisConnection = new SqlConnection(
             17                 @"Server=ta690g; Integrated Security=True; Database=AdventureWorks"
             18                 );
             19             thisConnection.Open();
             20 
             21             //創建SQL查詢命令行
             22             SqlCommand thisCommand = thisConnection.CreateCommand();
             23             thisCommand.CommandText = "select * from HumanResources.Department";
             24 
             25             //執行指定的查詢
             26             SqlDataReader thisReader = thisCommand.ExecuteReader();
             27 
             28             //讀出并顯示數據
             29             FileStream file = new FileStream( "result.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite );
             30             StreamWriter wfile = new StreamWriter( file );
             31 
             32             while ( thisReader.Read() )
             33             {
             34                 Console.WriteLine( "{0}\t{1}", thisReader[0], thisReader[1] );
             35                 wfile.WriteLine( "{0}\t{1}", thisReader[0], thisReader[1] );
             36             }
             37 
             38             wfile.Close();
             39             file.Close();
             40 
             41             thisReader.Close();
             42             thisConnection.Close();
             43         }
             44 
             45         static void Main2( string[] args )
             46         {
             47             //創建連接
             48             SqlConnection thisConnection = new SqlConnection(
             49                  @"Server=ta690g; Integrated Security=True; Database=AdventureWorks"
             50                 );
             51 
             52             //創建數據適配器
             53             SqlDataAdapter thisAdapter = new SqlDataAdapter(
             54                 "select * from HumanResources.Department", thisConnection
             55                 );
             56 
             57             //創建數據集合
             58             DataSet thisDataSet = new DataSet();
             59             thisAdapter.Fill( thisDataSet, "Department" );
             60 
             61             foreach ( DataRow row in thisDataSet.Tables["Department"].Rows )
             62             {
             63                 Console.WriteLine( row[0+ "\t" + row[1] );
             64             }
             65 
             66             thisConnection.Close();
             67         }
             68 
             69         static void Main( string[] args )
             70         {
             71             //創建連接
             72             SqlConnection thisConnection = new SqlConnection(
             73                  @"Server=ta690g; Integrated Security=True; Database=AdventureWorks"
             74                 );
             75 
             76             //創建DataAdapter對象 -- 進行更新操作
             77             SqlDataAdapter thisAdapter = new SqlDataAdapter(
             78                "select * from HumanResources.Department", thisConnection
             79                 ) ;
             80 
             81             //創建SQL命令
             82             SqlCommandBuilder thisBuilder = new SqlCommandBuilder( thisAdapter ) ;
             83 
             84             //創建數據集合DataSet來包含相關的數據表,rows, columns
             85             DataSet thisDataSet = new DataSet() ;
             86 
             87             thisAdapter.Fill( thisDataSet, "Dep" ) ;
             88             Console.WriteLine( thisDataSet.Tables["Dep"].Rows.Count );
             89             Console.WriteLine( "name = {0}", thisDataSet.Tables["Dep"].Rows[0]["Name"] ) ;
             90 
             91             ////更新元素
             92             //thisDataSet.Tables["Dep"].Rows[0]["Name"] = "wangzhaoren";
             93             //thisAdapter.Update( thisDataSet, "Dep" );
             94 
             95             ////thisAdapter.Fill( thisDataSet, "Dep" );
             96             //Console.WriteLine( "name = {0}", thisDataSet.Tables["Dep"].Rows[0]["Name"] );
             97 
             98             ////插入新元素
             99             //Console.WriteLine( thisDataSet.Tables["Dep"].Rows.Count );
            100             //DataRow thisRow = thisDataSet.Tables["Dep"].NewRow();
            101             //thisRow["Name"] = "lovefang";
            102             //thisRow["GroupName"] = "Research and Development";
            103             //thisDataSet.Tables["Dep"].Rows.Add( thisRow );
            104             //thisAdapter.Update( thisDataSet, "Dep" );
            105 
            106             //Console.WriteLine( thisDataSet.Tables["Dep"].Rows.Count );
            107            
            108             //設置主鍵
            109             DataColumn[] col = new DataColumn[1];
            110             col[0= thisDataSet.Tables["Dep"].Columns["DepartmentID"];
            111             thisDataSet.Tables["Dep"].PrimaryKey = col;
            112 
            113             DataRow row = thisDataSet.Tables["Dep"].Rows.Find( "17" );
            114             if ( row == null )
            115             {
            116                 Console.WriteLine( "not find" );
            117             }
            118             else
            119             {
            120                 Console.WriteLine( row[1] );
            121             }
            122 
            123 
            124             thisConnection.Close();
            125         }
            126     }
            127 }
            128 
            久久99精品久久久大学生| 91精品国产乱码久久久久久| 久久亚洲国产成人影院网站| 奇米影视7777久久精品人人爽| 久久久久亚洲av无码专区喷水| 久久91这里精品国产2020| 久久水蜜桃亚洲av无码精品麻豆| 精品久久人人妻人人做精品| 一级女性全黄久久生活片免费| 国产精品亚洲美女久久久| 国产成人精品白浆久久69| 久久婷婷五月综合97色一本一本| 久久伊人精品青青草原高清| 久久九九精品99国产精品| 久久国产视频99电影| 久久精品这里只有精99品| 亚洲va久久久噜噜噜久久天堂| 中文无码久久精品| 五月丁香综合激情六月久久| 成人精品一区二区久久久| 波多野结衣久久一区二区| 99久久亚洲综合精品成人| 久久久精品一区二区三区| 久久夜色精品国产噜噜亚洲a| 合区精品久久久中文字幕一区| 久久综合九色欧美综合狠狠| 久久久国产精品福利免费| 精品永久久福利一区二区| 国内精品人妻无码久久久影院导航| 久久精品国产一区二区| 麻豆精品久久精品色综合| 国产精品禁18久久久夂久| 久久综合给合久久狠狠狠97色| 中文字幕乱码久久午夜| 久久婷婷五月综合97色直播| 热99RE久久精品这里都是精品免费 | 日韩精品久久久肉伦网站| 伊人久久大香线蕉av不变影院| 久久国产免费直播| 婷婷久久久亚洲欧洲日产国码AV | 久久久老熟女一区二区三区|