dbconnection mysql,C#连接MySQL数据库报错:类型“System.Data.Common.DbConnection”在未被引用的程序集中定义的解决...

报错:

类型“system.data.common.dbconnection”在未被引用的程序集中定义。必须添加对程序集“system.data, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089”的引用。

我不小心删除了引用中的 system.data 重新添加就好了,或者重新创建一个项目,项目里默认会有。

附上相关代码:

using system;

using system.collections.generic;

using system.componentmodel;

using system.data;

using system.drawing;

using system.linq;

using system.text;

using system.threading.tasks;

using system.windows.forms;

using mysql.data;

using mysql.data.mysqlclient;

using system.collections;

using system.configuration;

using system.data.common;

private void button1_click(object sender, eventargs e)

{

string connstr = "server=localhost;user id=root;password=123456;database=company";

mysqlconnection mycon = new mysqlconnection(connstr);

mycon.open();

mysqlcommand mycmd = new mysqlcommand("insert into product(prod_id,prod_name) values('p1005','摄像机')",mycon);

if(mycmd.executenonquery()>0)

{

this.label1.text = "数据插入成功!";

}

mycon.close();

}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,确保你已经安装了 .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 文件,那么程序将使用该文件而不是创建一个新的文件。如果你想创建一个新的数据库文件,只需删除该文件即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值