SQLConnection.Open (); throwing exception

Updating the old piece of software, but to maintain backward compatibility I need to connect to the .mdb (access) database.

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

I checked the path, the existence of the database, etc., and that is all right.

            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;
            } 

Exception Message:

When connecting to SQL Server, a network-related or specific instance error occurred. The server was not found or was not available. Verify the instance name is correct and configure SQL Server to connect remotely. (provider: Named Pipes provider, error: 40 - Could not open SQL Server connection)

+3
source share
3

, SQL Server (SqlConnection) MDB Access???

Access, OleDbConnection. Access . http://www.connectionstrings.com/access

+4

:

Provider = Microsoft.Jet.OLEDB.4.0; = C:\mydatabase.mdb; = admin; =;

+2

, using... Database, System.IO.Path.Combine...:

            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;
-1
source

Source: https://habr.com/ru/post/1735843/


All Articles