简体   繁体   English

使用C#将Access DB连接到VS

[英]connecting Access DB to VS using C#

This is the 1st time I am using C# connection with db, also I dont use access. 这是我第一次使用C#与db连接,也不使用访问权限。 I just want to know the initial steps required to connect the access db to visual studio C# windows application. 我只想知道将访问数据库连接到Visual Studio C#Windows应用程序所需的初始步骤。 I have searched though the internet, It helped a lot. 我通过互联网进行搜索,这很有帮助。 I cant find my mistake in the code. 我在代码中找不到我的错误。 I think I am missing some steps in establishing the connection with the db. 我想我缺少与数据库建立连接的一些步骤。

here is the error that appears when I try to run the program "'\\f38910\\Users\\kainat.baig\\Desktop\\AAA\\Database101' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides." 这是当我尝试运行程序“'\\ f38910 \\ Users \\ kainat.baig \\ Desktop \\ AAA \\ Database101'不是有效路径时出现的错误。请确保路径名拼写正确且已连接文件所在的服务器。”

CODE: 码:

OleDbConnection bookConn;
OleDbCommand oleDbCmd = new OleDbCommand();

String connParam = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \\f38910\Users\kainat.baig\Desktop\AAA\Database101; Persist Security Info=False";

    public Form1()
    {
        bookConn = new OleDbConnection(connParam);
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bookConn.Open(); //*ERROR LINE
        oleDbCmd.Connection = bookConn;


        bookConn.Close();
     }

First your file name does not have the file extension name like .mdb 首先,您的文件名没有.mdb类的文件扩展名。

 Database101\MyDB.mdb

Second, if it is in a network. 其次,如果它在网络中。 Would it be better if you net use first in you DOS prompt to assign a drive letter for your network, like: 如果您首先在DOS提示符下net use来为网络分配一个驱动器号,那就更好了,例如:

 net use z: \\f38910\Users\kainat.baig\Desktop\AAA\Database101 your_password /USER:your_account

Then your connection string will be shorter then: 然后,您的连接字符串将短一些:

 String connParam = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= z:\MyDB.mdb; Persist Security Info=False";

If the file extension by the way is .accdb instead of .mdb it should be: 如果文件扩展名是.accdb而不是.mdb ,则应为:

 String connParam = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=z:\MyDB.mdb;;Persist Security Info=False;"

Or simply your path is simply wrong. 或者仅仅是您的路径是错误的。

Go to windows explorer and just clink on the path where your file is and copy paste it. 转到Windows资源管理器,然后仅在文件所在的路径上单击并复制并粘贴它。

You forget to add .mdb after database name or .accdb if you are using Microsoft Office Access 2007 or higher database but, for that you have to change Provider also. 如果您使用的是Microsoft Office Access 2007或更高版本的数据库,则忘记在数据库名称后添加.mdb.accdb ,但是为此您还必须更改提供程序。

String connParam = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \\f38910\Users\kainat.baig\Desktop\AAA\Database101.mdb; Persist Security Info=False";

Or you can use connection string like this 或者您可以使用这样的连接字符串

String connParam = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\f38910\Users\kainat.baig\Desktop\AAA\Database101.mdb;User Id=admin;Password=;"

EDITED: 编辑:

for ACCESS 2010 适用于ACCESS 2010

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\f38910\Users\kainat.baig\Desktop\AAA\Database101.accdb"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM