简体   繁体   English

如何将Microsoft Access数据库连接到可视C#?

[英]how to connect Microsoft Access Database to visual c#?

how to onnect Microsoft Access Database to visual c# 如何将Microsoft Access数据库连接到可视C#

for example: i make a database that have a table named "student" and fields " id , name " so i make ac# form that have 2 text boxs and a button "add" that add the contents of the two text box to the database 例如:我创建一个数据库,该数据库具有一个名为“ student”的表和字段“ id,name”,因此我使ac#表单具有2个文本框和一个“ add”按钮,该按钮将两个文本框的内容添加到数据库

bye 再见

Here's an overview of the process on MSDN you might take a look at. 这是您可以查看的有关MSDN上的过程的概述 Don't hesitate to ask if you encounter some specific problem implementing the solution. 不要犹豫,问您在实施该解决方案时是否遇到一些特定问题。

You will also need to have MDAC (Microsoft Data Access Components). 您还需要具有MDAC (Microsoft数据访问组件)。

In order to help you with the connection string and its parameters for a data file such as an Access database, please follow the following link specific to Access: Access . 为了帮助您获得Access文件之类的数据文件的连接字符串及其参数,请遵循以下特定于Access的链接: Access

For other connection strings in general: ConnectionStrings.com . 对于其他一般的连接字符串: ConnectionStrings.com

In short, you need to specify your complet filename to the Access database file in your connectionString. 简而言之,您需要在connectionString中为Access数据库文件指定complet文件名。

using (OleDBConnection connection = new OleDBConnection(connectiongString)) {
    if (connection.State != ConnectionState.Open)
        connection.Open();

    string sql = "INSERT INTO Student (Id, Name) VALUES (@idParameter, @nameParameter)"

    using (OleDBCommand command = connection.CreateCommand()) {
        command.CommandText = sql;
        command.CommandType = CommandType.Text;

        OleDBParameter idParameter = command.CreateParameter()
        idParameter.DbType = System.Int32;
        idParameter.Direction = Parameterdirection.Input;
        idParameter.Name = "@idParameter";
        idParameter.Value = studentId; // Where studentId is an int variable that holds your parsed TextBox.Text property value.

        OleDBParameter nameParameter = command.CreateParameter()
        // Do the same as you did above for the nameParameter.

        try {
            command.ExecuteNonQuery()
        } finally {
            command.Dispose();
            connection.Dispose();
        }
    }
}

Disclaimer This code is provided as-is as it was not compiled nor tested. 免责声明此代码按原样提供,未经编译或测试。 That is only to show you the idea of how it works. 那只是为了向您展示其工作原理。 Further tests might be necessary depending on your project architecture or else. 可能需要进一步测试,具体取决于您的项目体系结构。

You can use the ado.net database connection objects from within the System.Data.OleDb namespace. 您可以从System.Data.OleDb命名空间中使用ado.net数据库连接对象。 These object include the following 这些对象包括以下内容

OleDbConnection OleDbCommand OleDbDataReader OleDbConnection OleDbCommand OleDbDataReader

Additionally, here is the quick tutorial from Microsoft to get you up and running. 此外,这是Microsoft提供的快速教程,可帮助您入门和运行。

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual C#2010中使用Microsoft Access 2007数据库 - Using Microsoft Access 2007 database with Visual C# 2010 使用C#在Visual Studio中插入,更新和删除Microsoft Access数据库 - Microsoft Access Database, Insert, Update and Delete in Visual Studio with C# Visual C#和Microsoft Access 2007 - Visual C# And Microsoft Access 2007 使用Microsoft Access数据库引擎在Visual c#中评估Access计算字段时,数据类型不匹配 - Data Type Mismatch when evaluating Access Calculated Fields in Visual c# using Microsoft Access Database Engine 如何以编程方式在C#中创建Microsoft Access数据库? - How to create Microsoft Access database in C# programmatically? 如何在C#中打开与Microsoft Access数据库的连接 - How to open connection with Microsoft Access database in C# 如何将已发布的Visual C#解决方案连接到其他数据库 - How to connect published Visual C# solution to a different database 如何使用Visual C#连接到远程SQL Server数据库? - How to connect to remote SQL Server database using Visual C#? 如何将Oracle数据库连接到Visual Studio C#项目 - How to connect Oracle Database to Visual Studio C# project 如何将Postgres数据库与C#Visual Studio连接 - How to connect postgres database with c# visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM