Re: OleDbException for Opening a database
- From: "Brendan Reynolds" <brenreyn@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 11 Dec 2005 00:38:08 -0000
Here's an example that lists information about the error that occured. The
SQLState property of the OleDbError object returns 3024 for the 'file not
found' error, that would be one way to identify this specific error.
namespace caTest1
{
class Class1
{
[System.STAThread]
static void Main(string[] args)
{
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ @"Data Source=c:\usenet\NoSuchFile.mdb";
System.Data.OleDb.OleDbConnection cnn
= new System.Data.OleDb.OleDbConnection(strConnection);
try
{
cnn.Open();
}
catch (System.Data.OleDb.OleDbException ex)
{
System.Console.WriteLine(ex.Message + " (" + ex.ErrorCode.ToString() +
")");
foreach (System.Data.OleDb.OleDbError err in ex.Errors)
{
System.Console.WriteLine("Message: " + err.Message + "\n"
+ "Native Error: " + err.NativeError + "\n"
+ "Source: " + err.Source + "\n"
+ "SQL State: " + err.SQLState + "\n");
}
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
finally
{
if (cnn != null)
{
if (cnn.State != System.Data.ConnectionState.Closed)
{
cnn.Close();
}
}
}
System.Console.ReadLine();
}
}
}
--
Brendan Reynolds
"McFloyd" <McFloyd@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:6D4D01DC-53F8-4FEC-85C1-7C17B12170C2@xxxxxxxxxxxxxxxx
>I want to be able to handle the problem if my Access Database is not found,
> and create a new one if necessary. I was trying to find the "file not
> found"
> error from System.Data.OleDb.OleDbException but couldn't find it.
>
> The System.IO.FileNotFoundException doesn't work with this because it's a
> database
>
> Instead, retrieved the substring from the Message element and compared it
> with the actual "Could not find file" message, like this:
>
> if (odbException.Message.Substring(0,19).Equals("Could not find file"))
> {
> ...
> }
>
> Is this the only way you can handle this error? or I'm missing something.
>
> I appreciate your comments.
>
>
>
.
- Prev by Date: Re: Stored procedure output problem in ASP
- Next by Date: Re: Stored procedure output problem in ASP
- Previous by thread: Re: Stored procedure output problem in ASP
- Next by thread: ADO Connection - Fails - Unspecified Error
- Index(es):