C#实现SQLite数据库文件的读写操作示例

发表时间: 2021-12-15 16:05

读取SQLite数据库,就是读取一个路径\192.168.100.**\position\db.sqlite下的文件

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>

</startup>

这个放在</configuration>里面去

我在winform 里的代码

public void jiazaiweizhi()

{

string sql = "SELECT * FROM messages";

string connStr = @"Data Source=" + @"\192.168.100.**\position\db.sqlite;Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";

using (SQLiteConnection conn = new SQLiteConnection(connStr))

{

using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn))

{

DataSet ds = new DataSet();

adapter.Fill(ds);

DataTable dt = ds.Tables[0];

// MessageBox.Show(dt.Columns[1].ColumnName, "数据库信息");

textBox1.Text = "当前位置" + Environment.NewLine + dt.Rows[0][1].ToString();

}

}

}

.Net SQLite数据库驱动以及System.Data.SQLite.dll下载最新地址:

http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

(电脑上可以搜索System.Data.SQLite.dll这个dll,加载进入项目之后 可以看它的版本

SQLite管理工具下载地址:

http://www.cr173.com/soft/94247.html

App.config文件修改:

  1. <?xml version="1.0"?>
  2. <configuration>
  3. <appSettings>
  4. <!--FailIfMissing=false表示数据库不存在时就会自动创建-->
  5. <add key="DbSQLite" value="data source=|DataDirectory|DB.db3;Pooling=true;FailIfMissing=false"/>
  6. </appSettings>
  7. </configuration>

备注:如果开发环境是4.0,而System.Data.Sqlite是比较低的版本,则可能会弹出错误信息“混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集”,解决方法是在上面加上:

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>

</startup>

这个放在</configuration>里面去

数据库读写助手SqliteHelper.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Data.SQLite;


  8. public class SqliteHelper
  9. {
  10. public SqliteHelper()
  11. {
  12. //
  13. //TODO: 在此处添加构造函数逻辑
  14. //
  15. }

  16. private static SQLiteConnection GetConnection()
  17. {
  18. string connStr = System.Configuration.ConfigurationManager.AppSettings["DbSQLite"].ToString();
  19. SQLiteConnection conn = new SQLiteConnection(connStr);
  20. conn.Open();
  21. return conn;
  22. }

  23. public static int ExecuteSql(string sql)
  24. {
  25. using (SQLiteConnection conn = GetConnection())
  26. {
  27. var cmd = new SQLiteCommand(sql, conn);
  28. return cmd.ExecuteNonQuery();
  29. }
  30. }

  31. public static int ExecuteScalar(string sql)
  32. {
  33. using (SQLiteConnection conn = GetConnection())
  34. {
  35. var cmd = new SQLiteCommand(sql, conn);
  36. object o = cmd.ExecuteScalar();
  37. return int.Parse(o.ToString());
  38. }
  39. }
  40. public static SQLiteDataReader ExecuteReader(string sql)
  41. {
  42. SQLiteConnection conn = GetConnection();
  43. var cmd = new SQLiteCommand(sql, conn);
  44. SQLiteDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  45. return myReader;
  46. }
  47. public static DataSet ExecDataSet(string sql)
  48. {
  49. using (SQLiteConnection conn = GetConnection())
  50. {
  51. var cmd = new SQLiteCommand(sql, conn);
  52. SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
  53. DataSet ds = new DataSet();
  54. da.Fill(ds);

  55. return ds;
  56. }
  57. }
  58. }

窗体中应用Form1.cs

    1. //判断表是否存在,不存在则生成
    2. int result = SqliteHelper.ExecuteScalar("SELECT COUNT(*) FROM sqlite_master where type='table' and name='tb'");
    3. if (result == 0)
    4. {
    5. //创建表
    6. SqliteHelper.ExecuteSql("create table [tb] (id integer PRIMARY KEY autoincrement, [name] varchar(20), [createDate] datetime default (datetime('now', 'localtime')))");
    7. }
    8. //插入一行数据
    9. result = SqliteHelper.ExecuteSql("insert into [tb](name) values ('Luck')");
    10. if(result > 0)
    11. {
    12. string msg = "";
    13. //读取数据
    14. SQLiteDataReader dr = SqliteHelper.ExecuteReader("select * from [tb]");
    15. if (dr.Read())
    16. {
    17. msg += dr[0] + "," + dr[1] + "," + dr[2];
    18. }
    19. MessageBox.Show(msg);
    20. }