• C# 与 SQLite 的使用


    C# SourceCode Example

    C# 源码案例

    This Example shows the basic things when working with SQLite.NET.

    这个例子显示了使用SQLite.NET时的基本的事情。

    Basically this is how all ADO.NET Data Providers work ;D

    基本上,这是ADO.NET Data Providers 怎样工作的。

    Before you can use Finisar's SQLite.NET Data Provider, you have to tell your IDE about it.

    在你使用 Finisar's SQLite.NET Data Provider之前,你必须告诉你的IDE关于它的事情。

    This is how it is done in "Microsoft Visual C# 2005 Express Edition Beta 2" - It should not differ much in other IDEs...

    在"Microsoft Visual C# 2005 Express Edition Beta 2"中,它是这样做的 -  它与其他IDE没有不同。

    1) Place the .dll files you downloaded from this website into your projects binary folder.

    1)将你从网站下载的 System.Data.SQLite.dll 放到你项目目录的bin文件夹内。

    2) Press 'Project -> Add Reference...' in the menu bar.

    2)在菜单栏点击“项目->添加引用'

    3) Select the 'Browse'-Tab and select the "SQLite.dll".

    3)选择‘浏览’选择System.Data.SQLite.dll

    4) Press 'Ok' and you are finishd ;D

    4)点击'OK' 然后你就完成了。

    C# SourceCode:

    using Finisar.SQLite;
    // [snip] - As C# is purely object-oriented the following lines must be put into a class:
    // We use these three SQLite objects:
    SQLiteConnection sqlite_conn;
    SQLiteCommand sqlite_cmd;
    SQLiteDataReader sqlite_datareader;
    // create a new database connection:
    sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
    // open the connection:
    10 sqlite_conn.Open();
    11 // create a new SQL command:
    12 sqlite_cmd = sqlite_conn.CreateCommand();
    13 // Let the SQLiteCommand object know our SQL-Query:
    14 sqlite_cmd.CommandText = "CREATE TABLE test (id integer primary key, text varchar(100));";
    15 // Now lets execute the SQL ;D
    16 sqlite_cmd.ExecuteNonQuery();
    17 // Lets insert something into our new table:
    18 sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (1, 'Test Text 1');";
    19 // And execute this again ;D
    20 sqlite_cmd.ExecuteNonQuery();
    21 // ...and inserting another line:
    22 sqlite_cmd.CommandText = "INSERT INTO test (id, text) VALUES (2, 'Test Text 2');";
    23 // And execute this again ;D
    24 sqlite_cmd.ExecuteNonQuery();
    25 // But how do we read something out of our table ?
    26 // First lets build a SQL-Query again:
    27 sqlite_cmd.CommandText = "SELECT * FROM test";
    28 // Now the SQLiteCommand object can give us a DataReader-Object:
    29 sqlite_datareader=sqlite_cmd.ExecuteReader();
    30 // The SQLiteDataReader allows us to run through the result lines:
    31 while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
    32 {
    33 // Print out the content of the text field:
    34 System.Console.WriteLine( sqlite_datareader["text"] );
    35 }
    36 // We are ready, now lets cleanup and close our connection:
    37 sqlite_conn.Close();
    38
  • 相关阅读:
    bzoj 2152: 聪聪可可
    bzoj 2143: 飞飞侠
    bzoj 2132: 圈地计划
    bzoj 2127: happiness
    bzoj 2124: 等差子序列
    bzoj 2120: 数颜色
    对MySQL数据类型的认识
    MySQL详解--锁,事务(转)
    mysql 5.7快速部署
    elasticsearch报错[WARN ][bootstrap ] Unable to lock JVM Memory: error=12,reason=Cannot allocate memory,解决
  • 原文地址:https://www.cnblogs.com/skyler/p/2225145.html
Copyright © 2020-2023  润新知