简体   繁体   English

如何连接到 Visual Studio 中的访问数据库

[英]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.

相关问题 如何连接到Visual Studio中的数据库 - How can I connect to a database in Visual Studio 手动将 MS Access 数据库连接到 Visual Studio - Connect MS Access Database to Visual Studio manually 无法连接到Visual Studio中的MySQL数据库 - Can't connect to MySQL database in Visual Studio 如何从 Visual Studio 中的 GridView 更新 Access 数据库? - How do I update an an Access database from a GridView in Visual Studio? 如何在 Visual Studio 中连接到 Sqlite 并创建数据库? - How to connect to Sqlite in Visual Studio and create database? 在Visual Studio中创建安装文件时,为什么不能连接到SQL Server数据库? - Why can't I connect to the SQL Server database when I create setup file in Visual studio? 我无法从Visual Studio 2012连接到我的SQL Server 2014数据库 - I can't connect to my SQL Server 2014 database from Visual Studio 2012 如何在SQL Server Management Studio中使用Visual Studio创建连接数据库? - How connect database created in SQL Server Management Studio with Visual Studio? 如何在控制台应用程序上连接访问数据库? - How can i connect access database on console application? 在Visual Studio 2015中连接到数据库 - Connect to database in Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM