Access2007 OleDbConnection over network share persists although share is deletedH
I've encountered a quite strange behavior of ADO.NET / Access 2007.
I run my C# 2008 program (target framework .NET 2.0) on PC1. PC1 has a network share on PC2 (\PC2\Temp mapped as X:) On PC2 in Temp there is the access database file xy.mdb
The program opens a OleDbConnection to X:\xy.mdb. Works 开发者_JAVA技巧fine.
Then while the program is still running I drop the share on PC2. (Windows Explorer on PC1 tells me the share X: is lost) I renamed the database file on PC2, so no new connection should be possible.
But the program can still query the database ! (via OleDbCommand.ExecuteReader() or ExecuteNonQuery())
Has anyone an explanation for me ? Is the whole database latched ? And can I prevent this so that I get an OleDbException when the share is dropped and I try to query the no longer available database ?
Thanks for any help, Ralf
I tried to reproduce the issue with the following code and a sample access db...not matter how I tried (sahre or mapped drive) i always got the exception:
Unhandled Exception: System.Data.OleDb.OleDbException: The Microsoft Jet database engine cannot find the input table or query 'somejunk'. Make sure it exists and that its name is spelled correctly.
which is expected. You should re-examine you environment to be 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);
}
}
}
}
}
精彩评论