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
  • 相关阅读:
    ACM学习历程—SNNUOJ1132 余数之和(数论)
    ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)
    ACM学习历程—HDU4675 GCD of Sequence(莫比乌斯)
    ACM学习历程—HDU4746 Mophues(莫比乌斯)
    ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)
    ACM学习历程—HDU1695 GCD(容斥原理 || 莫比乌斯)
    ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)
    ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)
    ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)
    alert、confirm、prompt的区别
  • 原文地址:https://www.cnblogs.com/skyler/p/2225145.html
Copyright © 2011-2022 走看看