1

I am testing some things out in an Access DB (This will be going away, but until I document all functionality, its what I've got) and when I debug my code, I am getting 0 data back from my query. The connection opens just fine, just no data. Anything about the below code stick out to you guys?

Connection String

<connectionStrings>
  <add name="DefaultConnection" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Projects\\Facts\LoginNew2000.mdb; Persist Security Info=False;" providerName="System.Data.SqlClient" />
</connectionStrings>

Query, returning no data

OleDbConnection myAccessConn = null;
try
{
    myAccessConn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

    myAccessConn.Open();

    OleDbCommand myAccessCommand = new OleDbCommand("select User_Name from Managers");
    myAccessCommand.Connection = myAccessConn;
    myAccessCommand.Prepare();

    var rdr = myAccessCommand.ExecuteReader();
    while (rdr.Read())
    {
        var x = rdr[0].ToString();
    }
}
catch (Exception ex)
{
    Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
}
finally
{
    myAccessConn.Close();
}
return View();

The above code, while debugging, skips directly from the start of the while loop to the ending try block curly brace.

Baklap4
  • 3,914
  • 2
  • 29
  • 56
  • 1
    You're sure there's data available in the database? – Baklap4 Aug 08 '17 at 13:20
  • @Baklap4 Yep. When I run the select statement directly in Access, the data is returned as intended. This DB is rather old I should add, which is why I am testing some things to begin the update to something much more modern. Would the age of this DB (2002 - 2003ish) make a difference? –  Aug 08 '17 at 13:25
  • @DevNoob It would be surprising if it did. –  Aug 08 '17 at 13:32
  • 1
    @DevNoob I would begin by challenging your assumptions. Make absolutely sure you have the right path. Make sure the connection string at runtime is what you *think* it is. Make sure you are opening that same file in Access. –  Aug 08 '17 at 13:41
  • 1
    Agree with @Amy. I just tried your code and it works fine. – SQLAndOtherStuffGuy Aug 08 '17 at 13:42
  • Also you're not showing anything in your view, so if you're wondering why there's nothing in the view.. well add your results to your model and view access them in your razor views :) I've just checked your code and layed the docs beside them.. It looks alright (didn't test it) https://msdn.microsoft.com/en-us/library/aa288452(v=vs.71).aspx – Baklap4 Aug 08 '17 at 13:46
  • Ok. So, I never got the original DB to return any results. Can't figure out why. I created a new Access DB, used the connection string connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Went to "Tools -> Options -> Projects and Solutions -> Web Projects" and checked the "Use the 64 bit version of IIS Express for web sites and projects" and this returns data from my new test DB. Again, the old DB from 2002 - 2003 time frame DOES NOT return any data. Guess I'll keep playing with it. –  Aug 08 '17 at 14:28
  • Just an FYI OleDB provider support in 64bit applications is flaky: https://stackoverflow.com/questions/861022/oledb-not-supported-in-64bit-mode – Dimitri Aug 08 '17 at 14:35
  • @DevNoob I'm having trouble rectifying your earlier statement, "Yep. When I run the select statement directly in Access, the data is returned as intended." with your most recent statement, "So, I never got the original DB to return any results." You appear to be contradicting yourself. –  Aug 08 '17 at 14:47
  • OleDb is sometimes fussy about field names. It is worth trying "SELECT [User_Name] FROM Managers" – Jonathan Willcock Aug 08 '17 at 14:58

0 Answers0