Ado
programming in Ado.net by Andrew R. Phillips
submitted by
User Level
Date of submissionAndrew R. phillipsintermediate03/19/2001.
Programming in C # with Ado.net are much easier than it sounds. Microsoft has made it simpler for anyone to connect. Ado.net has the Adodata class and if you are using a SQL Server Database with can use Sqldataset classed for betrer speed and performance.
The the "the" the "thing" to "is" add the following code to the top of the code form.
Using System.Data;
The next thing you need to a ADO connection resource and a ADO Command resource. Can do this two ways. The the ' Form.cs ' of the ' way is ' from the ' Toolbox click on the ' Data components and drag and drop an adoconnect Ion and Adodatasetcommand objects into the form.
The following code is added to your the code form.
Private System.Data.ADO.ADODataSetCommand AdoDataSetCommand1;
Private System.Data.ADO.ADOConnection AdoConnection1;
Next you need to make a connection to the database using the adoconnection and th Adodatasetcommand objects. The connection and command string would look like this:
String sconn = "server=localhost;uid=sa;pwd=;d atabase=northwind";
String Scommand = "Select FirstName, LastName from Employees";
You are then need to connect to the database. This is doing by calling a new Adodatasetcommand object using the command and connection string as the parameters.
Adodatasetcommand dscmd = new Adodatasetcommand (Scommand, sconn);
So far the "ado.net has look" a lot like the ADO we have come to love and adore. Once we have made the database connection and have queried the database we are ready for something new. The DataSet Object. The DataSet object. The DataSet Object is further than just an expanded RecordSet. With the DataSet Object, connect to a copy of a database table (s) or even a entire database. Also you are able to read and write to a XML files.
by calling FillDataSet () of the Adodatasetcommand your are able to load a DataSet with data from a database.
DataSet ds = new DataSet ();
Dscmd.filldataset (DS);
Next you need to create a new DataTable and connecting it to your dataset with the using the method Tables of the DataSet class . DataTable class has several other methods two of which are Columns and Constraints, which returns a collection of Columns and constraints respectively.
DataTable dt = ds. tables["EMPLOYEES"];
You are are now ready to get the data from the database. You can use the Foreach function along and a DataRow object to access the data quite easily.
foreach (DataRow dr in Dt. Rows)
{
LISTBOX1.ITEMS.ADD (dr["FirstName"] + ":" +
dr["LastName"]};
}
To put the "data into" a grid all of you need to do are to create a data grid in the "design mode" and then after the DSCMD.FILLD Ataset (ds, 揈 mployees), use the following code
Datagrid1.datasource=ds. tables[揈 mployees 擼. DefaultView; ]
This would put the data from the DataSet directly into the Datagrid.
The Complete Code:.
Namespace WindowsApplication4
{
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.WinForms;
Using System.Data;
<summary>
Summary description for Form1.
</summary>
public class Form1:System.WinForms.Form
{
<summary>
Required designer variable.
</summary>
Private System.ComponentModel.Container components;
Private System.Data.ADO.ADODataSetCommand AdoDataSetCommand1;
Private System.Data.ADO.ADOConnection AdoConnection1;
Private System.WinForms.ListBox ListBox1;
Public Form1 ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();
DataSet ds = new DataSet ();
Adodatasetcommand1.filldataset (DS);
DataTable dt = ds. tables["TREASURY"];
foreach (DataRow dr in Dt. Rows)
{
LISTBOX1.ITEMS.ADD (dr["base_last_updated"] + ":" +
dr["Base_yield"] + "" +dr["COUPON"]);
}
}