[英]How can i connect to an access database in Visual Studio
i have a hospital solution in visual studio 2019. i have completed the design, i need to connect to a my access database.我在 Visual Studio 2019 中有一个医院解决方案。我已经完成了设计,我需要连接到我的访问数据库。
i have this code to try to achieve that我有这个代码来尝试实现这一点
try
{
string constring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Acer\Desktop\Haigazian\Major_Courses\DataBase\My Project\HospitalDB.accdb";
OleDbConnection conDataBase = new OleDbConnection(constring);
String x = textBox1.Text;
OleDbCommand cmdSelectParcel = new OleDbCommand("Select * from Employee where SSN ='" + x + "'", conDataBase);
...
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
I also have this at the top of the form using System.Data.OleDb;
我也在
using System.Data.OleDb;
however, this isn't being used and is greyed out.然而,这并没有被使用并且是灰色的。
Whenever I write OleDbConnection
it gives me an error and says每当我写
OleDbConnection
它给我一个错误并说
"the type name 'OleDbConnection' could not be found in the namespace 'System.Data.OleDb'. this type has been forwarded to assembly 'System.Data.OleDb, Version=4.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' Consider adding a reference to that assembly"
“在命名空间 'System.Data.OleDb' 中找不到类型名称 'OleDbConnection'。此类型已转发到程序集 'System.Data.OleDb,版本 = 4.0.1.0,文化 = 中性,PublicKeyToken = 31bf3856ad364e35'考虑添加对该程序集的引用”
I have copied the code from another solution file and the System.Data.OleDb
works perfectly over there so it must have something to do with my project specifically.我已经从另一个解决方案文件中复制了代码,并且
System.Data.OleDb
在那里完美运行,因此它必须与我的项目特别有关。 I have also tried to add the database access through View>Other Windows>Data Sources but I receive the message我也尝试通过 View>Other Windows>Data Sources 添加数据库访问,但我收到了消息
"this window is not supported for the selected project. For steps to enable data binding, please visit: https://aka.ms/WinForms/DataBinding"
“所选项目不支持此 window。有关启用数据绑定的步骤,请访问: https://aka.ms/WinForms/DataBinding”
and I have checked out the website but I haven't been able to figure out.我已经查看了该网站,但我无法弄清楚。
Does anyone have any idea how to fix this issue?有谁知道如何解决这个问题?
You need a reference in your project to System.Data.您需要在项目中引用 System.Data。
Incidentally, once you have that done you need to open the database with..顺便说一句,一旦你完成了,你需要用..打开数据库。
conDataBase.Open();
You should also parameterize your query rather than passing in values in the query string to avoid SQL Injection...您还应该参数化您的查询,而不是在查询字符串中传递值以避免 SQL 注入...
DataTable Employees = new DataTable();
using (OleDbCommand cmdSelectParcel = new OleDbCommand("Select * from Employee where SSN = ?", conDataBase))
{
cmdSelectParcel.Parameters.Add("?", OleDbType.VarChar).Value = x;
Employees.Load(cmdSelectParcel.ExecuteReader());
}
You'll also need to do something with the data when you get it...当你得到它时,你还需要对数据做一些事情......
foreach (DataRow Employee in Employees.rows)
{
//Do Something
}
Check that your project actually has a reference to the Microsoft OleDb stuff.检查您的项目是否确实引用了 Microsoft OleDb 内容。 eg right click your project in solution explorer and select add->reference->COM then look in the list for your Microsoft OleDB stuff and tick them.
例如,在解决方案资源管理器中右键单击您的项目并 select add->reference->COM 然后在列表中查找您的 Microsoft OleDB 内容并勾选它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.