C#操做Dataset數據集與SQLite數據庫

1、SQLite操做,直接貼代碼,很簡單:sql

 //建立一個數據庫文件
    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.SQLiteConnectionStringBuilder 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());

2、利用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);
相關文章
相關標籤/搜索