尽管共享已删除,但Access2007网络共享上的OleDbConnection仍然存在

时间:2011-02-16 16:53:43

标签: ms-access ado.net ms-access-2007

我遇到了ADO.NET / Access 2007的一个非常奇怪的行为。

我在PC1上运行我的C#2008程序(目标框架.NET 2.0)。 PC1在PC2上有一个网络共享(\ PC2 \ Temp映射为X :) 在Temp中的PC2上有访问数据库文件xy.mdb

程序打开OleDbConnection到X:\ xy.mdb。工作正常。

然后,当程序仍在运行时,我将分享放在PC2上。 (PC1上的Windows资源管理器告诉我共享X:丢失) 我在PC2上重命名了数据库文件,因此不可能有新的连接。

但是程序仍然可以查询数据库! (通过OleDbCommand.ExecuteReader()或ExecuteNonQuery())

有人对我有解释吗? 整个数据库是否已锁定? 我可以阻止这种情况,以便在删除共享时我得到OleDbException并尝试查询不再可用的数据库吗?

感谢您的帮助, 拉尔夫

1 个答案:

答案 0 :(得分:0)

我尝试使用以下代码和示例访问数据库重现问题...无论我如何尝试(sahre或映射驱动器)我总是得到例外:

  

未处理的例外情况:   System.Data.OleDb.OleDbException:The   Microsoft Jet数据库引擎不能   找到输入表或查询   'somejunk'。确保它存在并且   它的名字拼写正确。

这是预料之中的。你应该重新检查你的环境是100%。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace AccessOverShare
{
    class Program
    {
        static void Main(string[] args)
        {
            int ct = 0;

            using(OleDbConnection oleDbConnection = new OleDbConnection())
            {
                //oleDbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\\\127.0.0.1\\share\\test.mdb.";
                oleDbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\\test.mdb.";
                oleDbConnection.Open();

                using(OleDbCommand oleDbCommand = oleDbConnection.CreateCommand())
                {
                    oleDbCommand.CommandType = CommandType.Text;
                    oleDbCommand.CommandText = "select junkid, junktext from somejunk";

                    using (OleDbDataReader oleDbDataReader = oleDbCommand.ExecuteReader(CommandBehavior.Default))
                    {
                        ct = 0;         
                        while(oleDbDataReader.Read())
                        {
                            ct++;
                            Console.WriteLine("{0:0000}", oleDbDataReader["junkid"]);
                        }
                    }

                    Console.WriteLine();
                    Console.WriteLine(ct);
                }

                Console.WriteLine();
                Console.WriteLine();

                Console.Write("kill the share then press enter to continue");
                Console.ReadLine();

                using (OleDbCommand oleDbCommand = oleDbConnection.CreateCommand())
                {
                    oleDbCommand.CommandType = CommandType.Text;
                    oleDbCommand.CommandText = "select junkid, junktext from somejunk";

                    using (OleDbDataReader oleDbDataReader = oleDbCommand.ExecuteReader(CommandBehavior.Default))
                    {
                        ct = 0;
                        while (oleDbDataReader.Read())
                        {
                            ct++;
                            Console.WriteLine("{0:0000}", oleDbDataReader["junkid"]);
                        }
                    }

                    Console.WriteLine();
                    Console.WriteLine(ct);
                }
            }
        }
    }
}