none
Access database looks empty in Windows service RRS feed

  • Question

  • I have a block of code that will run just fine in a Windows Forms application, but fails in an odd way in a Windows service. I haven't been able to find any information directly related to this faliure.

    When running in a service, the rdr.Read() returns null, where it returns the one row in the table when running in a forms app.

    Does anyone have some insight to this? In a forms app, the code writes a file with "Foo bar" in it. The service writes a file with just "Foo" in it.

    Any help would be greatly appreciated.

    string

     

    rc = "";

     

    OleDbConnection settingsConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:/TestDB.mdb");

     

    string sql = "select * from TestTable";

    settingsConn.Open();

     

    OleDbCommand cmd = new OleDbCommand(sql, settingsConn);

     

    OleDbDataReader rdr = cmd.ExecuteReader();

     

    while (rdr.Read()) // <-- This acts like there are no rows when its in a service.

    {

    rc = rdr[

    "Message"].ToString();

    }

    settingsConn.Close();

     

    TextWriter tw = new StreamWriter("c:/test.txt");

    tw.WriteLine(

    "Foo " + rc);

    tw.Close();

    Tuesday, October 26, 2010 3:12 PM

Answers

  • Most likely it is a permission issue. When Windows Service runs it is hosted under some service account, which (in most cases) is not the same as user account. If account does not have proper permissions to access resources, code would fail. What I would recommend is not put exception handling inside of the code and log all the errors using Trace class. It would give you some ideas about what is going on in your code.


    Val Mazur (MVP) http://www.xporttools.net
    Tuesday, October 26, 2010 10:08 PM
    Moderator

All replies

  • Rather difficult to answer. I don't see any error handling (Try...Catch) or logging so you can't really tell what might be happening.

    Paul ~~~~ Microsoft MVP (Visual Basic)
    Tuesday, October 26, 2010 3:38 PM
  • Most likely it is a permission issue. When Windows Service runs it is hosted under some service account, which (in most cases) is not the same as user account. If account does not have proper permissions to access resources, code would fail. What I would recommend is not put exception handling inside of the code and log all the errors using Trace class. It would give you some ideas about what is going on in your code.


    Val Mazur (MVP) http://www.xporttools.net
    Tuesday, October 26, 2010 10:08 PM
    Moderator