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
Solved

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

Posted on 2004-08-29
1
1,161 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

856 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