473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

C# Database connection code

Can anyone help me with the meaning of this snippet?


Expand|Select|Wrap|Line Numbers
  1. string conString =@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=c:\billing.mdb";
  2. OleDbConnection conn = new OleDbConnection(conString);
  3. Conn.Open();
  4.  
  5. DataSet ds = new DataSet();
  6. OleDbDataAdapter adapter =
  7. new OleDbDataAdapter("Select * from product where id=" + id, conn);
  8. adapter.Fill(ds);
  9.  
  10. conn.Close();
  11.  
  12. DataTable dt = ds.Tables[0];
  13. foreach (DataRow dr in dt.Rows)
  14. {
  15.    data = Convert.ToInt32(dr["count"]);
  16. }
Oct 11 '11 #1
2 1688
Rabbit
12,516 Expert Mod 8TB
It connects to a database, queries it, and puts the field count into the variable data. However, the data variable keeps getting replaced with the current row's data so you're only ever going to get the last row of data.
Oct 11 '11 #2
Frinavale
9,735 Expert Mod 8TB
This line:
Expand|Select|Wrap|Line Numbers
  1. string conString =@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source=c:\billing.mdb";
Declares a string variable called "conString" that contains information on for connecting to a database. This type of string is referred to as a Connection String.

The next line declares and creates a new instance of a OleDbConnection. The OleDbConnection is used to connect to a database. It requires the Connection String in order to know how to connect to the database.
Expand|Select|Wrap|Line Numbers
  1. OleDbConnection conn = new OleDbConnection(conString);
The next line opens the connection to the database.
Expand|Select|Wrap|Line Numbers
  1. Conn.Open();
The next lines create a new instance of a DataSet. A DataSet is a collection of DataTables. These are fill with information retrieved from the database using a DataAdapter.

Expand|Select|Wrap|Line Numbers
  1.  
  2. DataSet ds = new DataSet();
  3. OleDbDataAdapter adapter =
  4. new OleDbDataAdapter("Select * from product where id=" + id, conn);
  5. adapter.Fill(ds);
After executing the DataAdapter's Fill method, the DataSet is filled with DataTables that are populated with data retrieved from the database. In this case the SQL command executed is very simple and only 1 table is involved...therefore the DataSet will only contain 1 DataTable.


The following line closes the connection to the database because you're done working with the database (you've retrieved the data you need to work with)
Expand|Select|Wrap|Line Numbers
  1. conn.Close();

In the following code, you are retrieving the 1 DataTable within the DataSet that was created for you by the DataAdpter.

Then you are looping through each row/record in that DataTable and retrieving the data stored in the "count" field/cell.

For some reason you're converting this into a .NET Integer type and are storing this into a variable called "data". I don't know why you're doing this...the data variable will continue to be replaced while looping....


Expand|Select|Wrap|Line Numbers
  1. DataTable dt = ds.Tables[0];
  2. foreach (DataRow dr in dt.Rows)
  3. {
  4.    data = Convert.ToInt32(dr["count"]);
  5. }
So, the result of this code is that "data" is filled with the "count" information for the last row/record in the data table retrieved from the database.

Whenever you don't understand what a control is in a piece of code you should refer to the MSDN Library. This library contains documentation, articles, walk-throughs on all of the .NET controls and programming concepts. I have added links to each control and concept that was used in the code you posted. In the future please use the Search function of the MSDN Library to research a control before asking about it.

-Frinny
Oct 11 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Poewood | last post by:
Does anyone have some basic code and the corresponding resources needed that will enable me to connect to an Access Database in .net compact frmwrk using C#. I need to learn how to create a...
3
by: R Reyes | last post by:
Hi, I'm trying to modularize my database connections a little better and get more out of my project with less code. First check out this common dbOpen() function inside class clsDatabase. I...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
7
by: Bernie Yaeger | last post by:
I can't believe that there aren't lots of developers who: 1. create a crystal report that connects to sql server 2. calls the report using the crystalreportviewer control to view it and then,...
3
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
5
by: Matt | last post by:
Hello, What is the best way to handle the database connection string for a class library project that will be compiled and used as a .dll? This .dll will be accessed via classic ASP and in...
4
by: Mike P2 | last post by:
Hi. I'm writing controls that have to query the database, and it bothers me that I might have several of these controls on a page that each create, open, and close their own connection with the...
3
by: Hahn, Thomas | last post by:
Hallo, I have an ASP.NET application with masterpages, skins and diffrent themes. The application works fine, but the performance is not realy good. If I load an ASPX file, which has no database...
3
eboschi
by: eboschi | last post by:
Hi all, i'm new to .net programming and i have some problems with database connection. I have written two web application. Both of them use ADODB Connection to Sql Server 2000 database. The problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.