CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    10

    Question C# + Access Database

    This may be an easy question, but it's been so long since I've dealt with Access that I've been beating my head against the wall trying to get a simple query to work with C#.
    The Query is "SELECT # FROM Personnel WHERE (myName = \"Test\") AND (myPassword = \"test\")
    I can run the query directly from Access and it returns the row I want, but when I run it through the code, I get the "ODBC Microsoft Access Driver - too few parameters. Expected 1" error.
    I'm accessing the database through a DSN.
    Any ideas why this will not work?

  2. #2
    Join Date
    Oct 2010
    Posts
    10

    Re: C# + Access Database

    FYI - I'm working with VS 2008...

  3. #3
    Join Date
    Jun 2009
    Posts
    144

    Re: C# + Access Database

    here http://www.connectionstrings.com/access you can find how to connect to access, but why do you choose to do this with odbc ?
    Last edited by invader7; February 1st, 2011 at 04:00 PM.

  4. #4
    Join Date
    Oct 2010
    Posts
    10

    Re: C# + Access Database

    I'm using ODBC just because that's what I've always used when connecting to an Access database. However, the last time I did that, I was using VB.NET 2003, and now I'm coding in C# 2008...
    I've been to ConnectionStrings.com, and I'm fairly certain that the connection itself is ok. Here is the steps I use:

    private OdbcDataAdapter adapter;
    private OdbcConnection conn;
    private OdbcCommand com;
    public DataSet ds = new DataSet();
    public int numResults;

    try
    {
    conn = new OdbcConnection(connectionString);
    adapter = new OdbcDataAdapter(sql, connectionString);


    adapter.Fill(ds,"Results");

    numResults = ds.Tables["Results"].Rows.Count;
    results = ds.Tables["Results"];
    }

    ** connectionString = "DSN=myDatabase" - this DSN is set and working correctly
    ** sql = Select string listed above
    ** The program always crashes on the "adapter.Fill(ds, "Results") section

  5. #5
    Join Date
    Jun 2009
    Posts
    144

    Re: C# + Access Database

    i use this whith access

    Code:
    using System.Data.OleDb;
    ...               
    			   try
                    {
                        OleDbConnection thisConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.accdb;Mode=ReadWrite;Jet OLEDB:Database Password=mypassword;");
                        OleDbCommand thisCommand = new OleDbCommand("your query here", thisConnection);
    
                        thisConnection.Open();
                        OleDbDataReader thisReader = thisCommand.ExecuteReader();
    
                        while (thisReader.Read())
                        {
                            //this will read the value thisReader.GetValue(0).ToString()
                        }
    
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

  6. #6
    Join Date
    Oct 2010
    Posts
    10

    Re: C# + Access Database

    Thanks for your help; that code works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured