views:

111

answers:

3

Updating an old piece of software but in order to maintain backward compatibility I need to connect to a .mdb (access) database.

I am using the following connection but keep getting an exception, why?

I have validated the path, database existence etc. and that is all correct.

            string Server = "localhost";
            string Database = drive + "\\btc2\\state\\states.mdb";
            string Username = "";
            string Password = "Lhotse";

            string ConnectionString = "Data Source = " + Server + ";" +
                                      "Initial Catalog = " + Database + ";" + 
                                      "User Id = '';" + 
                                      "Password = " + Password + ";";

            SqlConnection SQLConnection = new SqlConnection();

            try
            {
                SQLConnection.ConnectionString = ConnectionString;
                SQLConnection.Open();
            }
            catch (Exception Ex)
            {
                // Try to close the connection
                if (SQLConnection != null)
                    SQLConnection.Dispose();

                //
                //can't connect
                //

                // Stop here
                return false;
            } 

The exception message is:

{"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}

+2  A: 

You need to add a data provider to your connection string:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;

Joe Pitz
Is SQLConnection correct as well or should it be OLEDB connection as here? http://msdn.microsoft.com/en-us/library/aa288452%28VS.71%29.aspx
Martin Smith
SQLConnection is fine, just need the correct provider. I edited ths connection string for .net
Joe Pitz
Yes you could use OleDbConnection. I am so used to using sqlServer.
Joe Pitz
Actually SqlConnection class is only for SQL Server connections. To connect to OleDb providers, use OleDbConnection. SqlConnection will not work.
Remus Rusanu
Yep, you are totally right, I'm going to go with Joe in that I am so used to using sqlServer connections at work that I totally forgot about an Access database being anything different...Thanks.
flavour404
Who was first, Joe or Remus?? Or 'who wants the credit?'
flavour404
+2  A: 

What exactly makes you think that a SQL Server connection (SqlConnection) will be willing to connect to an MDB Access database???

If you connect to Access, use OleDbConnection. For Access connection strings, see http://www.connectionstrings.com/access

Remus Rusanu
According to this, Remus you were first with the correct answer... thanks :)
flavour404
A: 

try this more safer version using the using clause...as for the Database variable, it would be better and safer to use the System.IO.Path.Combine method...:

            string Server = "localhost";
            string Database = System.IO.Path.Combine(@"C:\", @"\btc2\state\states.mdb");

            string Username = "";
            string Password = "Lhotse";

            string ConnectionString = "Data Source = " + Server + ";" +
                                      "Initial Catalog = " + Database + ";" + 
                                      "User Id = '';" + 
                                      "Password = " + Password + ";";
            bool bDatabaseOk = false;
            using (SqlConnection SQLConnection = new SqlConnection()){

                try
                {
                    SQLConnection.ConnectionString = ConnectionString;
                    SQLConnection.Open();
                    bDatabaseOk = true;
                }
                catch (SqlException Ex)
                {
                   // Handle the SqlException here....
                   //
                   //can't connect
                    bDatabaseOk = false;
                }
            } 
            return bDatabaseOk;
tommieb75
Originally when the question was posted...I was thinking that the problem was in the connection, so I wrapped it up using the using clause and used a more safer means of IO.Path.Combine....and changed the Exception to SqlException to get the exact error!
tommieb75
thanks tommie I will take a look and keep this 'safer' way of doing it in mind.
flavour404