Connecting to Access Database in Code Behind

Hi All,

What would be the standard approach to connecting to and manipulating data within an Access database from code-behind (C#)?

Thanks,

Rit
LVL 1
rito1Asked:
Who is Participating?
 
cfEngineersConnect With a Mentor Commented:

public Form1()
{
    InitializeComponent();
    // initiate DB connection
    string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;
        Data Source=mydbname.mdb";
    try
    {
        database = new OleDbConnection(connectionString);
        database.Open();
        //SQL query to list movies
        string queryString = "SELECT ID, FName, LName
            FROM mytable WHERE ID = 1";
        loadDataGrid(queryString);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return;
    }
}

Open in new window

0
 
GeoffSuttonCommented:
Except that the MessageBox won't show anything since client server doesn't allow for that sort of interaction :)  Usually I just dump exception data to a text file  - System.IO.File.Append("Errors.txt",ex.Message + "\r\n");

cfEngineers deserves the points though :)  I am just making an observation.
0
 
rito1Author Commented:
Brilliant, thanks.

Rit
0
 
cfEngineersCommented:
Glad to help, and thanks Geoff for that correction.
0
 
GeoffSuttonCommented:
No worries :)  I code almost entirely in web apps, so don't think in terms of messagebox :)  You had a far more accurate answer than I would have given.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.