Solved

how to connect to a mdb via odbc with c# in a webapplication

Posted on 2004-08-29
1
1,162 Views
Last Modified: 2008-03-10
can anybody give me an example of connecting to a ms access database via odbc in c#.

i want to know how i can simply fill some textboxes of a webapp out of a database with some SELECT-queries.

thanxs
h
0
Comment
Question by:1hoschi
1 Comment
 
LVL 35

Accepted Solution

by:
YZlat earned 125 total points
ID: 11926194
first create odbc for your access database. Give it a name, for example MyODBC. Then go to the root directory of your C# application and find web.config file. Open it and add the following to <configuration section, right below</system.web> section:

 <appSettings>
      <add key="dsn" value="dsn=MyODBC;" />
  </appSettings>

Then create a module, something like MyFunctions.cs file with the following code:



    Public OdbcConnection GetConnection()
{
        string strDSN= ConfigurationSettings.AppSettings("dsn");
        OdbcConnection* Conn = new OdbcConnection(strDSN);        
        Conn.Open();

        Return Conn;
}

    Public OdbcDataReader GetData(string sql )
{
        OdbcConnection* conn= GetConnection();

      OdbcCommand* cmd = new OdbcCommand(sql, conn);
      OdbcDataReader* dr = cmd->ExecuteReader();

      Return dr;
}

Then from any of your web forms' code-behind files you can declare a datareader, sql string and simply call GetData function:

string sql="Select * from Table1";
OdbcdataReader dr=GetData(sql);

While dr->Read(){
/*retrieve the data and display it*/
txtName.Text=dr["FullName"]

}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

713 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