-1

Additional information: 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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) da.SelectCommand = cmd; da.Fill(ds);

The code:

  public string LogNotification { get; set; }
            public bool ConfirmLogin(string id, string pw)
            {
                using (SqlConnection con = new SqlConnection("Data Source=C:\\RegistrationMDB.mdb"))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    SqlCommand cmd = new SqlCommand("SELECT ID, PASSWORD FROM Students WHERE ID = @ID OR PASSWORD = @PASSWORD", con);
                    da.SelectCommand = cmd;
                    da.Fill(ds);
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = id;
                    cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar).Value = pw;






                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        iD = (dr["@ID"].ToString());
                        password = dr["@PASSWORD"].ToString();
                    }

                    if (iD == id && password == pw)
                    {
                        return true;
                    }
                    else
                    {
                        LogNotification = "ID/Password is incorrect";
                        return false;
                    }
                }
            }

Database Code;

 //++++++++++++++++  DATABASE Data Elements +++++++++++++++++
        public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
        public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
        public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
        public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
        public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
        public System.Data.OleDb.OleDbConnection OleDbConnection;
        public String cmd;

        public void DBSetup(){
        // +++++++++++++++++++++++++++  DBSetup function +++++++++++++++++++++++++++
        // This DBSetup() method instantiates all the DB objects needed to access a DB, 
        // including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand, 
        // oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each
        // Command object contains a Connection object and an SQL string object.
            OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
            OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
            OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
            OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
            OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
            OleDbConnection = new System.Data.OleDb.OleDbConnection();


            OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
            OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
            OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
            OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;


OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+
"istry Path=;Jet OLEDB:Database L" + 
"ocking Mode=1;Data Source=C:\\RegistrationMDB.mdb;J" + 
"et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" + 
"ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" + 
"hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " + 
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" + 
"r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

        }

        public void SelectDB(String id) 
        { //++++++++++++++++++++++++++  SELECT +++++++++++++++++++++++++
            DBSetup();
            cmd = "Select * from Students where ID = " + iD;
            OleDbDataAdapter.SelectCommand.CommandText = cmd;
            OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  {
                    OleDbConnection.Open();
                    System.Data.OleDb.OleDbDataReader dr;
                    dr = OleDbDataAdapter.SelectCommand.ExecuteReader();

                    dr.Read();
                    id=iD;
                    setPassword(dr.GetValue(1)+"");
                    setEMail(dr.GetValue(2)+"");

                    setGpa(Double.Parse(dr.GetValue(3)+""));
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

        public void InsertDB() {
        // +++++++++++++++++++++++++++  INSERT +++++++++++++++++++++++++++++++

            DBSetup();
            cmd = "INSERT into Students values(" + getID() + "," +
                             "'" + getPassword() + "'," +
                             "'" + getEMail() + "'," +
                            "'" + getGpa() +  ")";

            OleDbDataAdapter.InsertCommand.CommandText = cmd;
            OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Inserted");
                else
                    Console.WriteLine("ERROR: Inserting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                
        }
        public void updateDB() 
        {
            //++++++++++++++++++++++++++  UPDATE  +++++++++++++++++++++++++

            cmd = "Update Students set ID = '" + getID() + "'," + 
                        "Password = '" + getPassword() +    "', " +
                        "Email = '" + getEMail() + "', " +
                         "GPA = " + getGpa();

            OleDbDataAdapter.UpdateCommand.CommandText = cmd;
            OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Updated");
                else
                    Console.WriteLine("ERROR: Updating Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

        public void deleteDB() 
        {
            //++++++++++++++++++++++++++  DELETE  +++++++++++++++++++++++++

            cmd = "Delete from Students where ID = " + getID();
            OleDbDataAdapter.DeleteCommand.CommandText = cmd;
            OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Deleted");
                else
                    Console.WriteLine("ERROR: Deleting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }
Woman
  • 1
  • 2
  • This is a code dump an an error message. The message is quite specific, what have you done to verify that you can locate the SQL server and that your connection string is correct? – Ron Beyer Apr 18 '15 at 02:09
  • I opened the visual studios, opened the code and put the database into the server, I tested it but the ds.fill is giving me the error now. It says failed to connect to database – Woman Apr 18 '15 at 02:13
  • Shouldn't it be a single backslash? C:\RegistrationMDB.mdb – Amir Apr 18 '15 at 02:15
  • @Amir - No, \\ means an escape sequence for a single slash, you can use a single slash only with a @ sign in front of the string, otherwise its interpreted as a control character. – Ron Beyer Apr 18 '15 at 02:17
  • why did you do `add.Parameters`, after `da.Fill(ds)`? – Bondaryuk Vladimir Apr 18 '15 at 02:17
  • I honestly wrote down what my professor told me to do and typed it up. – Woman Apr 18 '15 at 02:57

1 Answers1

0
  • Looking at your connection string, it appears that you are trying to directly open an MS Access database (.MDB) file using a SqlConnection object.

This raises a couple of issues.

  1. It sounds like you don't have a correct connection string for your Access database. Here's a link to some suggestions of how to setup your connection string depending on the technology that you're using:

https://www.connectionstrings.com/access/

  1. Also, as was mentioned below, it appears that you're using an incorrect connection type. Please see this related question for more information:

SQL connection string for microsoft access 2010 .accdb

Community
  • 1
  • 1
John Hodge
  • 1,645
  • 1
  • 13
  • 13
  • 1
    You can't connect to an Access database using SqlConnection, so thats not the only problem. – Ron Beyer Apr 18 '15 at 02:17
  • Exactly where does OP mention _"access database"_" –  Apr 18 '15 at 02:23
  • The part where it shows a connection string pointing to a .MDB file? – John Hodge Apr 18 '15 at 02:24
  • Ah, so he does, with a `SqlConnection` no less which you mentioned in your edit. Can you do a minor format edit (maybe add a _bullet point_) or something on your answer and I'll fix the downvote –  Apr 19 '15 at 06:34
  • I've edited as per your request. Let me know if I should clarify further. – John Hodge Apr 20 '15 at 03:48