使用SqlDataReader连接到本地.mdb文件的正确方法?

时间:2012-03-07 05:26:05

标签: c# sql database ms-access sqldatareader

即使连接在trycatch中,连接也会在不生成错误消息的情况下超时。我怀疑连接字符串有问题。这就是我目前所拥有的:

            string path = @"C:\PATH\TO\wantedDB.mdb";

            if (!File.Exists(path))
                throw new FileNotFoundException("File not found.");
            else
                Console.WriteLine("File found."); // File is found, so nothing wrong with that.

            string connectionstring = "Database=wantedDB;AttachDBFilename=" +
            path + ";Server=(local)";

            using (SqlConnection conn = new SqlConnection(connectionstring))
            {
                Console.WriteLine("Opening connection...");
                conn.Open();
                Console.WriteLine("Connection opened."); // Program never gets here.

我还尝试了连接字符串中的关系路径,如:

string connectionstring = "Database=wantedDB;AttachDBFilename=\"wantedDB.mdb\";Server=(local)";

db不受密码保护。我安装了MS Access,这会以某种方式影响这个吗?我错过了什么?

1 个答案:

答案 0 :(得分:1)

要连接到mdb文件,您应该使用OLEDB连接器:

var con = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=C:\\wantedDB.mdb;");