?
Solved

Connecting to Access Database in Code Behind

Posted on 2011-05-02
5
Medium Priority
?
383 Views
Last Modified: 2012-06-27
Hi All,

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

Thanks,

Rit
0
Comment
Question by:rito1
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
cfEngineers earned 2000 total points
ID: 35507178

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
 
LVL 10

Expert Comment

by:GeoffSutton
ID: 35507224
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
 
LVL 1

Author Comment

by:rito1
ID: 35507228
Brilliant, thanks.

Rit
0
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35507740
Glad to help, and thanks Geoff for that correction.
0
 
LVL 10

Expert Comment

by:GeoffSutton
ID: 35510072
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question