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