Connecting to an Access 2003 database from Visual Studio 2008 (1 Viewer)

Sigma248

Registered User.
Local time
Today, 06:44
Joined
Oct 6, 2008
Messages
24
How do I connect to an Access 2003 or 2007 database from Visual Studio 2008. I found the following code on a website and have tried it but get 1 error to do with the connection string:

protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con;
OleDbCommand cmd;
OleDbDataAdapter myDA;
DataSet myDataSet;

con = OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Swantrax.mdb; Persist Security Info=False;");


con.Open();

cmd = OleDbCommand(
"SELECT * From Parts", con);

myDA = OleDbDataAdapter(cmd);

myDataSet = DataSet();

myDA.Fill(myDataSet,
"MyTable");

DataGridView1.DataSource = myDataSet.Tables(
"MyTable").DefaultView;

con.Close();

con = Nothing;
}


When I buid the code I get an error with the connection string:

OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Swantrax.mdb; Persist Security Info=False;");

It does not like the Source string.

Any ideas?

 

Sigma248

Registered User.
Local time
Today, 06:44
Joined
Oct 6, 2008
Messages
24
I have made some progress with the code it is now:

protected
void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\\Swantrax_2003.mdb; Persist Security Info=False;");

con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * From tParts where tParts.PartNo=CRUSHER", con);
OleDbDataAdapter myDA = new OleDbDataAdapter(cmd);
DataSet myDataSet = new DataSet();

myDA.Fill(myDataSet,
"MyTable");
GridView1.DataBind();

con.Close();
}

Still having problems getting the data to display in the webpage.

Any ideas?

 

Users who are viewing this thread

Top Bottom