1. 程式人生 > >近日有需要寫點C#程式,有用到Dataset資料集和SQLite資料庫,由於我從來就不擅長記各種程式語言的語法,所以在查閱一堆資料後,留下以下內容備忘: 一、SQLite操作,直接貼程式碼,很簡單

近日有需要寫點C#程式,有用到Dataset資料集和SQLite資料庫,由於我從來就不擅長記各種程式語言的語法,所以在查閱一堆資料後,留下以下內容備忘: 一、SQLite操作,直接貼程式碼,很簡單

近日有需要寫點C#程式,有用到Dataset資料集和SQLite資料庫,由於我從來就不擅長記各種程式語言的語法,所以在查閱一堆資料後,留下以下內容備忘:

  一、SQLite操作,直接貼程式碼,很簡單:

      //建立一個數據庫檔案

    string  datasource=Application.StartupPath   "\\test.db" ;
    System.Data.SQLite.SQLiteConnection. CreateFile (datasource);
    //連線資料庫
    System.Data.SQLite.SQLiteConnection conn =


        new  System.Data.SQLite. SQLiteConnection ();
    System.Data.SQLite.SQLiteConnectionStringBu ilder connstr =
        new  System.Data.SQLite. SQLiteConnectionStringBuilder ();
    connstr.DataSource = datasource;
    connstr.Password =  "admin" ; //設定密碼,SQLite ADO.NET實現了資料庫密碼保護
    conn.ConnectionString = connstr.
 ToString ();
    conn. Open ();
    //建立表
    System.Data.SQLite.SQLiteCommand cmd =  new  System.Data.SQLite. SQLiteCommand ();
    string  sql =  "CREATE TABLE test(username varchar(20),password varchar(20))" ;
    cmd.CommandText=sql;
    cmd.Connection=conn;
    cmd. ExecuteNonQuery ();
    //插入資料
    sql = 
 "INSERT INTO test VALUES('dotnetthink','mypassword')" ;
    cmd.CommandText = sql;
    cmd. ExecuteNonQuery ();
    //取出資料
    sql =  "SELECT * FROM test" ;
    cmd.CommandText = sql;
    System.Data.SQLite.SQLiteDataReader reader = cmd. ExecuteReader ();
    StringBuilder sb =  new  StringBuilder ();
    while  (reader. Read ())
    {
        sb. Append ( "username:" ). Append (reader. GetString ( 0 )). Append ( "\n" )
            . Append ( "password:" ). Append (reader. GetString ( 1 ));
    }
    MessageBox. Show (sb. ToString ());

  二、利用Dataset資料集向SQLite資料庫插入資料,也直接貼程式碼:

     DialogResult dlgResult= openFileDialog1. ShowDialog ();   // 開啟要匯入的檔案
     if  (openFileDialog1.FileName ==  ""  || dlgResult != DialogResult.OK)
         return ;
     // 利用StreamReader類讀取文字內容
    StreamReader sr= new  StreamReader
        (File. OpenRead (openFileDialog1.FileName),System.Text.Encoding.Default);
     //連線資料庫
    System.Data.SQLite.SQLiteConnection conn =  new  System.Data.SQLite. SQLiteConnection ();
    System.Data.SQLite.SQLiteConnectionStringBuilder connstr
        =  new  System.Data.SQLite. SQLiteConnectionStringBuilder ();
    connstr.DataSource = datasource;
    connstr.Password =  "admin" ; //設定密碼,SQLite ADO.NET實現了資料庫密碼保護
    conn.ConnectionString = connstr. ToString ();
    conn. Open ();
     //大量更新時採用事務的方式,先快取事務,然後SQLiteDataAdapter.update後批量commit
    SQLiteTransaction ts = conn. BeginTransaction ();
     string  sql= " select name,number from test limit 1" ;
    SQLiteDataAdapter dta =  new   SQLiteDataAdapter (sql,conn);
    SQLiteCommandBuilder scb =  new   SQLiteCommandBuilder (dta);
    dta.InsertCommand=scb. GetInsertCommand ();
    DataSet DS =  new   DataSet ();
    dta. FillSchema (DS,SchemaType.Source,  "Temp" );     //載入表架構 注意
    dta. Fill (DS, "Temp" );     //載入表資料
    DataTable DT = DS.Tables[ "Temp" ];
     //插入資料
     while  (!sr.EndOfStream)
    {
         string [] strArr = sr. ReadLine (). Split ( new  Char[] {  '\t'  });
         if  (strArr[ 0 ] != ""  && strArr[ 1 ] != "" )
        {
            DataRow DR = DT. NewRow ();
            DR[ 0 ]=strArr[ 0 ];
            DR[ 1 ]=strArr[ 1 ];
            DT.Rows. Add (DR);
        }
    }
     int  result=dta. Update (DT);     // 如不用BeginTransaction和Commit批量提交事務,效能會很低,350條資料20多秒
    ts. Commit ();     // 提交事務
    DS. AcceptChanges ();
     // 釋放資源
    dta. Dispose ();
    DS. Clear ();
    conn. Close ();
    conn. Dispose ();
    sr. Close ();
    sr. Dispose ();
    MessageBox. Show ( "成功匯入了: "  + result. ToString () +  " 行資料。" ,  "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

相關推薦

no