ADO.NET using C#

Simple windows application using ADO.NET to make connection and display information in data grid.


Begin your first ADO.NET application by launching VS.NET and creating a new project using File - > Project and choose C# Windows Application template as shown in figure below:


Add a new item application configuration file as shown below from Project -> Add New Item:


The code for the application configuration file:


<?xml version="1.0" encoding="utf-8" ?>



    <add name="constr" connectionString="initial catalog=puran; data source=MCN002; integrated security=sspi"/>





Note: In the above code I have made a connection using windows authentication.


In the form design view drop a DataGrid and a button. Name the button as Fill.


DataGrid is used to display multiple tables (MARS - Multiple Active Result Sets concept is used).


Double click the Fill Button to come to form.cs file.


Add the necessary reference file from the solution explorer – Reference as below:


The code for the application is as:


using System;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Configuration;


namespace ADO_example1


    public partial class Form1 : Form


        public Form1()





        SqlDataAdapter da;

        DataSet ds;


        private void button1_Click(object sender, EventArgs e)


            da = new SqlDataAdapter("select * from testpuran", ConfigurationManager.ConnectionStrings["constr"].ConnectionString);

            ds = new DataSet();

            da.Fill(ds, "testpuran");

            dataGrid1.DataSource = ds;

            da.SelectCommand.CommandText = "select * from emp";

            da.Fill(ds, "emp");

            dataGrid1.DataSource = ds;





Output of the above program



Note: In the above article you have to change the database server, connection string and query statements accordingly.




Thought its a simple program but will help you in understanding basic of ADO.NET connection and displaying information.


Your feedback and constructive contributions are welcome.  Please feel free to contact me for feedback or comments you may have about this article.

Similar Articles