test if OleDbConnection is successful

sde

Well-known member
Joined
Aug 28, 2003
Messages
160
Location
us.ca.fullerton
I dont know if it is me or not, but this always fails. Does this look correct?

Code:
string connString = @"Provider=Microsoft.Jet.OLEDB.4.0; User Id=; " +
  @"Password=; Data Source=C:\test.xls; Extended Properties=Excel 8.0;"; 

OleDbConnection con = new OleDbConnection(connString);

if(con.State == ConnectionState.Open)
{
  MessageBox.Show("Connection to Database Successful");
  con.Close();
}
else
{
  MessageBox.Show("connection to Database Failed");
}
 
I dont believe you are actually opening the connection, so the state is still closed.

Try this:

C#:
try
{
   con.Open()
   // do your stuff here
}
finally
{
   con.Close()
}
 
Back
Top