Reader Level:
ARTICLE

A RoadMap For ADO.NET

Posted by faruk cubukcu Articles | ADO.NET September 20, 2010
Shows the basics of Microsoft ADO .NET.
  • 0
  • 0
  • 6841

In this article I would like to show the basic steps of using specific objects of Microsoft ADO .NET. Most of them are used in typical database application for example recording customer form or show the customer information on a datagrid.

Let's think about a flowchart which shows the objects and operations, and the usage orderly.

aroadmapforADONET.jpg

Figure: Flowchart for choosing and using ADO .NET objects.

Firstly, connection object must be specified. Connection object specifies the server, database and the authentication information.

A typical declaration for SQL Server connection string is:

Data Source = FARUK; Initial Catalog = FCELEKTRONIK; Integrated Security = true;

In this declaration: SQL Server instance name: FARUK, Database name: FCELEKTRONIK

Secondly, perform the data operations:

In this stage we must decide how the data will be accessed. It can be direct or indirect. In other words "connected" or "disconnected".

DataSet objects are used generally in a disconnected style. A DataSet is a buffer which stores data just like a database that we navigate for data or processing data. For example create a dataset then run a SQL select statement against it. Fill the customer records to dataset then populate a datagrid from the dataset which is done by DataAdapter object.

A typical example:

[Visual C#]

            string conStr = @"Data Source=FARUK ;Initial Catalog=FCELEKTRONIK;Integrated Security=True";
            string sql = "SELECT * FROM CUSTOMERS";
            SqlConnection con = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter dataadapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            con.Open();
            dataadapter.Fill(ds, "CUSTOMERS");
            con.Close();
            dataGridView1.DataSource = ds.Tables[0].DefaultView;

Note that the connection never opened and closed. Because this operations done by DataAdapter automatically.

How about the connected style? Command objects are used generally in this style. Connection is opened then a SQL Operation is done directly. For example an INSERT INTO statement. These operations are directly made on the database; that is why they are called "direct". This means database is opened, operation is done and database is closed manually.

Typical adding (insert):

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=FARUK; Initial Catalog=FCELEKTRONIK ;Integrated Security=True;";
            // open connection
            try
            {
                conn.Open();
                // command - insert
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO COSTOMERS (CODE, NAME, AMOUNT) " +
                "VALUES (@CODE, @NAME, @AMOUNT)";
                cmd.Parameters.Add("@CODE", SqlDbType.VarChar, 10).Value = textBox1.Text;
                cmd.Parameters.Add("@NAME", SqlDbType.VarChar, 10).Value = textBox2.Text;
                cmd.Parameters.Add("@AMOUNT", SqlDbType.Decimal).Value = textBox3.Text;
                cmd.ExecuteNonQuery(); // run the command
                MessageBox.Show("FC- successful added!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }

In this example Parameters are used instead of real data or textboxes. Using parameters is the way of more secure data that will be added to database.

How about the execution of a SELECT statement on a database or specifically reading data sequentially (read only)? This is generally done by the DataReader object.

Typical example is (reading data and populating listbox):

[Visual C#]

            SqlConnection con = new SqlConnection();
            con.ConnectionString =
            @"Data Source=FARUK;Initial Catalog=FCELEKTRONIK;Integrated Security=True";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            cmd.CommandText = "Select * from CUSTOMERS";
            SqlDataReader dr = default(SqlDataReader);
            dr = cmd.ExecuteReader();
            while (dr.Read)
            {
                this.lstFCCustomers.Items.Add(dr.GetValue(1));
            }
            dr.Close();
            con.Close();

I hope this helps you to use ADO .NET objects.

Faruk

Article Extensions
Contents added by ramanakanth k on Sep 20, 2010
For Connection Oriented you may have to use DataReader concept and For Connection Less You have to work on DataSet concept, Comparing to DataSet, Data Reader is More Faster But for dataset you may have to store bulk data.

Here is Some example for Souce Code:

Example for Connection Less:

SqlConnection Cn=new SqlConnection("user id="abc";pwd="xyz";database="sample";data source="urservername");
cn.open();
SqlDataAdapter da=new SqlDataAdapter("Select * from employee",cn);
DataSet ds=new DataSet();
da.fill(ds,employee);
Gridview1.DataSource=ds;
Gridview1.DataBind();

Example for Connection Oriented:

SqlConnection Cn=new SqlConnection("user id="abc";pwd="xyz";database="sample";data source="urservername");
cn.open();
SqlCommand cmd=new SqlCommand ("Select * from employee",cn);
SqlDataReader dr=cmd.ExecuteReader();
if(dr.Read)
{
Gridview1.DataSource=ds.Tables[0].DefaultView;
Gridview1.DataBind();
}
else
{
Response.Write("Data Not Exist");
}
COMMENT USING

Trending up