C# 命名空间“System.Data”中不存在类型或命名空间名称“SQLite”。是否缺少程序集引用

    因为项目需要,要读取客户电脑上面的SQLite数据库。客户电脑的程序使用的是C#开发,因此本人为了方便也使用C#集成SQLite的程序集。

    初始开发环境是visual studio 2010,.net framework版本是4.0 。引入 System.Data.SQLite.dll(此程序集是客户电脑上面的dll) 后,编译项目,结果就报:命名空间“System.Data”中不存在类型或命名空间名称“SQLite”。是否缺少程序集引用”。

    然后尝试各种方法,始终编译不成功。因为 .net framework版本是4.0,又专门在SQLite官网http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki上下载了对应的SQLite版本。重新编译,还是报错。

    最后,查看客户电脑安装  .net framework 版本,发现用的是4.7.2。因此又专门下载了 .net framework4.7.2,并把visual studio 的版本升级为2019,再次引入System.Data.SQLite.dll后,终于编译成功。

    这里简单说下,如果需要在Visual Studio中连接SQLite数据库,就必须选择合适的SQLite安装包。如果版本不一致,是编译不过去的。

首先,确保你已经安装了 .NET Framework 4.7.2 版本。然后,按照以下步骤来创建 SQLite 数据库和表,并将数据库文件路径指定为 Debug 文件夹: 1. 打开 Visual Studio 并创建一个新的控制台应用程序项目。 2. 在解决方案资源管理器中,右键单击项目并选择“管理 NuGet 程序包”。 3. 在 NuGet 程序包管理器中,搜索并安装“System.Data.SQLite.Core”包。 4. 打开 Program.cs 文件,并添加以下代码: ```csharp using System.Data.Entity; using System.Data.SQLite; namespace SQLiteDemo { class Program { static void Main(string[] args) { // 设置数据库文件路径为 Debug 文件夹 string dbPath = @"Data Source=.\Debug\mydatabase.sqlite;Version=3;"; // 创建 SQLite 数据库连接 SQLiteConnection connection = new SQLiteConnection(dbPath); // 创建 DbContext 实例 MyDbContext dbContext = new MyDbContext(connection); // 创建数据表 dbContext.Database.ExecuteSqlCommand("CREATE TABLE IF NOT EXISTS Customers (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT)"); // 添加数据 dbContext.Customers.Add(new Customer { Name = "John" }); dbContext.Customers.Add(new Customer { Name = "Mary" }); dbContext.SaveChanges(); // 查询数据 var customers = dbContext.Customers.ToList(); foreach (var customer in customers) { Console.WriteLine(customer.Name); } Console.ReadLine(); } } // 定义实体类 public class Customer { public int Id { get; set; } public string Name { get; set; } } // 定义 DbContext public class MyDbContext : DbContext { public MyDbContext(DbConnection connection) : base(connection, true) { } public DbSet<Customer> Customers { get; set; } } } ``` 5. 运行程序,你应该可以看到控制台输出以下内容: ``` John Mary ``` 6. 在 Debug 文件夹中,你应该可以看到一个为 mydatabase.sqlite 的文件。这就是你的 SQLite 数据库文件。 请注意,如果你在运行程序之前已经创建了 mydatabase.sqlite 文件,那么程序将使用该文件而是创建一个新的文件。如果你想创建一个新的数据库文件,只需删除该文件即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值