Solved

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

Posted on 2004-08-29
1
1,164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

688 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