0

I'm working on a program that gets data from a database on an .mdf file and populates a listbox with the results. I'm getting an error that says "An OLE DB Provider was not specified in the ConnectionString."

I can't seem to figure out what is the correct string to use for a provider. In my particular case, what am I supposed to do to get it to read the .mdf correctly?

            sConnection = "Data Source=.\\SQLEXPRESS; AttachDbFilename = StudentData.mdf;";
            dbConn = new OleDbConnection(sConnection);
            dbConn.Open();
            sql = "SELECT * FROM StudentData;";

            dbCmd = new OleDbCommand();
            dbCmd.CommandText = sql;

            dbCmd.Connection = dbConn;

            dbReader = dbCmd.ExecuteReader();
            while (dbReader.Read())
            {
                aMember = new
                    Member(dbReader["FirstName"].ToString(),
                    dbReader["LastName"].ToString());
                this.OutputListBox.Items.Add(aMember);
            }
            dbReader.Close();
            dbConn.Close();

Update: I've changed the connection string to:

sConnection = "Provider = SQLNCLI11;" +
                "Data Source = (LocalDB)/MSSQLLocalDB;" +
                "AttachDbFilename = \"c:/users/tevin/documents/visual studio 2015/Projects/DbReader/DbReader/StudentData.mdf\";" +
                "Connect Timeout = 30;";

However now I am getting the errors "Invalid authorization specification" and "Invalid connection string attribute".

shrimppunk
  • 49
  • 6

1 Answers1

0

The value of sConnection seems to be wrong. In this case you should use something like this:

Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;

For more information please visit conectionstrings.com

3vts
  • 778
  • 1
  • 12
  • 25