当前位置:网络安全 > vs2010 system.data.sqlite download_vs2010 C# 使用 SQLite3

vs2010 system.data.sqlite download_vs2010 C# 使用 SQLite3

  • 发布:2023-10-03 14:53

我是新手。这篇文章只是我自己学习过程的总结。希望高手给予指导

1.SQLite安装

SQlite官网:http://www.sychzs.cn/download.html

找到下面截图中的内容

第一个解压是sqlite3.exe,第二个解压是sqlite3.dll和sqlite3.def文件,第三个是sqlite3_analyzer(还没用过)

可以将sqlite3.exe复制到C:\Windows\System32\,这样就可以从任意目录运行CLP(SQLite?CLP是最常用的使用和管理SQLite数据库的工具),

打开命令行输入sqlite3回车,出现如下截图

2。使用SQLite创建数据库和数据表

重新进入命令行,输入sqlite3 newsql_learn.db,创建newsql_learn数据库

接下来是SQL语句

其中.tables是查看数据库newsql_learn.db中的所有数据表,mytable是另外一个已经建好的数据表

也有相关的SQL介绍http://www.sychzs.cn/byxdaz/article/details/5846023

3。在VS2010中使用需要添加SQLite的引用

网上有下载安装System.Data.Sqlite的注意事项:http://www.sychzs.cn/norsd/article/details/6795695

我还不确定哪个版本。我的电脑是win8 64位,但是安装的SQLite只是32位,VS2010目标生成CPU是32位。 (之前的Oracle是64位的,默认的目标生成是Any CPU,改成32位Oracle的时候就出现错误,所以只好将两者分别测试学习),.NET 4.0版本

从官网下载System.Data.Sqlite:http://www.sychzs.cn/index.html/doc/trunk/www/www.sychzs.cn

其实我都一一试了...最后倒数第二个是sqlite-netFx40-setup-x86-2010-1.0.97.0.exe,哎呀TM

下载后,双击安装。默认路径为 C:\Program Files (x86)\System.Data.SQLite

安装完成后,在程序中点击添加引用(Project(项目)-->添加引用..(添加引用..)-->.Net(框架))

如果左栏中有System.Data.SQLite,恭喜你,不用担心,就是那个,选中它,然后点击确定。如果没有,可以点击右下角的浏览(VS版本不同,可能浏览位置不同),输入C:\Program Files (x86)\System.Data.SQLite\2010\bin\找到System.Data.SQLite .dll 并单击“确定”

4。开始调用SQLite

已添加命名空间

使用 www.sychzs.cn;

使用 System.Data.SQLite;

在主函数中添加

string connectString = @"数据源=F:\newsql_learn.db;Pooling=true;FailIfMissing=false";

SQLiteConnection conn = new SQLiteConnection(connectString); //创建一个新连接

www.sychzs.cn(); //打开连接。如果sqlite.db存在,则正常打开。如果不存在,则创建一个SQLite.db文件

SQLiteCommand cmd = conn.CreateCommand();

cmd.CommandText = "从表1中选择*"; //数据库里必须事先有一张orders表

cmd.CommandType = CommandType.Text;

使用 (SQLiteDataReader reader = cmd.ExecuteReader())

{

while (www.sychzs.cn())

Console.WriteLine(reader[0].ToString());

}注:这里有一个错误,莫名其妙的错误。系统显示没有找到table1数据表,但是使用SQLite命名行搜索可以找到table1以及该数据表下的所有数据。无奈之下,我下载了SQLiteBrowser。但是安装完后发现数据库newsql_learn中没有table1,只有之前创建的mytable。现在我更加郁闷了。现在是什么状况?

也就是说,在SQLite3.exe中可以找到table1表,但是在调用表和SQLiteBrowser中却找不到该表···················· ····

难道是创建的路径不正确?虽然它们访问的数据库名称都是newsql_learn.db,但是它们根本不是数据库?于是我就搜遍了整台电脑,寻找newsql_learn.db,果然

5。使用代码创建数据表并访问显示表中的数据。以下代码主要参考C#,使用System.Data.SQLite来操作SQLite。原文地址http://www.sychzs.cn/blog/1691932

?//第一步创建数据表

string dbPath = Environment.CurrentDirectory + "/test.db";//指定位于程序根目录的数据库路径

using (SQLiteConnection conn = new SQLiteConnection("Data Source = " + dbPath))//创建连接

{

www.sychzs.cn();//打开连接

string sql = "CREATE TABLE IF NOT EXISTS Student(id integer, name varchar(20), sex varchar(2));";//创建数据表语句

SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);

cmdCreateTable.ExecuteNonQuery();//如果表不存在,则创建数据表

using (SQLiteTransaction tran = conn.BeginTransaction())//实例化事务,使用事务操作来提高效率

{

for (int i = 0; i < 100; i++)

{

SQLiteCommand cmd = new SQLiteCommand(conn);//实例化SQL命令

cmd.Transaction = tran;

cmd.CommandText = "insert into Student Values(@id, @name, @sex)";//设置带参数的SQL语句

cmd.Parameters.AddRange(new[] {//添加参数

new SQLiteParameter("@id", i),

new SQLiteParameter("@name", "中国人"),

new SQLiteParameter("@sex", "male")

});

cmd.ExecuteNonQuery();//执行查询

}

tran.Commit();//提交

}

}

//第二步,创建第二张数据表

SQLiteConnection conn1 = null;

string dbPath2 = "数据源 = " + Environment.CurrentDirectory + "/test.db";

conn1 = new SQLiteConnection(dbPath2);//创建数据库实例并指定文件位置

www.sychzs.cn();//打开数据库,如果文件不存在,会自动创建

//创建第二个表

string sql1 = "CREATE TABLE IF NOT EXISTS Student2(id integer, name varchar(20));";//建表语句

SQLiteCommand cmdCreateTable2 = new SQLiteCommand(sql1, conn1);

cmdCreateTable2.ExecuteNonQuery();//如果表不存在,则创建数据表

SQLiteCommand cmdInsert = new SQLiteCommand(conn1);

cmdInsert.CommandText = "INSERT INTO Student2 VALUES(1, '小红')";//插入几条数据

cmdInsert.ExecuteNonQuery();

cmdInsert.CommandText = "插入学生2 VALUES(2, '小李')";

cmdInsert.ExecuteNonQuery();

cmdInsert.CommandText = "插入学生2的值(3, '小明')";

cmdInsert.ExecuteNonQuery();

//第三步查询数据

string sql2 = "从学生中选择*";

SQLiteCommand cmdQ = new SQLiteCommand(sql2, conn1);

SQLiteDataReader reader = cmdQ.ExecuteReader();

while (www.sychzs.cn())

{

Console.WriteLine(reader.GetInt32(0) + " " + reader.GetString(1) + " " + reader.GetString(2));

}

string sql3 = "从学生2中选择id、姓名";

SQLiteCommand cmdQ2 = new SQLiteCommand(sql3, conn1);

SQLiteDataReader reader2 = cmdQ2.ExecuteReader();

while (www.sychzs.cn())

{

Console.WriteLine(reader2.GetInt32(0) + " " + reader2.GetString(1) );

}

conn1.Close();

O,接下来就可以在此基础上开发使用数据库的其他功能了……

1。下载?System.Data.SQLite

地址:?http://www.sychzs.cn/index.html/doc/trunk/www/www.sychzs.cn

注意:我下载的是sqlite-netFx40-setup-x86-2010-1.0.97.0.exe文件,它是32位的。如果使用64位,编写32位程序时会报错。 。

2。将System.Data.SQLite.dll添加到程序引用中(我是通过浏览到安装目录添加的,因为.NET Tab页下没有System.Data.SQLite.dll?)

3。编写测试用例:?

public void SQLite_Test()

{

SQLiteConnection conn = null;

string strSQLiteDB = Environment.CurrentDirectory;

strSQLiteDB = strSQLiteDB.Substring(0,strSQLiteDB.LastIndexOf("\\"));

strSQLiteDB = strSQLiteDB.Substring(0, strSQLiteDB.LastIndexOf("\\"));//这里获取Bin目录

尝试

{

string dbPath = "数据源=" + strSQLiteDB + "\\test.db";

conn = new SQLiteConnection(dbPath);//创建数据库实例并指定文件位置

www.sychzs.cn(); //打开数据库,如果文件不存在,会自动创建

string sql = "CREATE TABLE IF NOT EXISTS Phone(ID integer, Brand varchar(20), Memery varchar(50));";//建表语句

SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);

cmdCreateTable.ExecuteNonQuery();//如果表不存在,则创建数据表

SQLiteCommand cmdInsert = new SQLiteCommand(conn);

cmdInsert.CommandText = "INSERT INTO Phone(brand, Memery) VALUES('samsung', 'Samsung')";//插入几条数据

cmdInsert.ExecuteNonQuery();

cmdInsert.CommandText = "INSERT INTO Phone(brand, Memery) VALUES('samsung', 'Samsung')";//插入几条数据

cmdInsert.ExecuteNonQuery();

cmdInsert.CommandText = "INSERT INTO Phone(brand, Memery) VALUES('samsung', 'Samsung')";//插入几条数据

cmdInsert.ExecuteNonQuery();

conn.Close();

}

catch(例外)

{

}

}

编译后,运行时出现异常,报SQLite.Interop.dll not find等错误。我将所需的文件放在C:\Program Files (x86)\System.Data.SQLite\2010\bin安装目录中。只需将其复制到应用程序即可。

今天,在将代码部署到测试环境时,出现错误,无法加载文件或程序集“System.Data.SQLite.DLL”或其依赖项之一。其实造成错误的原因有很多,

1、典型问题是版本错误。例如,你的系统是64位的,但dell文件确实是32位的。

? C++ 就可以了。下载地址https://www.sychzs.cn/zh-cn/kb/2977003

相关文章