-
February 1st, 2011, 03:54 PM
#1
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?
-
February 1st, 2011, 03:56 PM
#2
Re: C# + Access Database
FYI - I'm working with VS 2008...
-
February 1st, 2011, 03:58 PM
#3
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.
-
February 1st, 2011, 04:07 PM
#4
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
-
February 1st, 2011, 04:08 PM
#5
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);
}
-
February 2nd, 2011, 09:01 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|