Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

737 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