zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    阿里云云效技术专家分享:云原生开发、调测及可靠发布解决方案
    对话李飞飞,揭秘国际体育赛事风“云”背后的黑科技
    时序数据库永远的难关 — 时间线膨胀(高基数 Cardinality)问题的解决方案
    当Java遇上机密计算,又一段奇幻之旅开始了!
    内核热补丁,真的安全么?
    在 Dubbo3.0 上服务治理的实践
    CCF-201509-3-生成模板系统
    WPF CommandParameter的使用
    UWP App Data存储和获取
    在WPF中的ItemsControl中使用事件和命令(Using events and Commands within ItemsControl in WPF)
  • 原文地址:https://www.cnblogs.com/skyler/p/2225145.html
Copyright © 2011-2022 走看看