ADO.NET framework helps you to connect to a Datasource i.e. Excel,MS-Access,MS-SQL,Oracle ETC
ADO is active data objects which contains classes for all the sources and methods to get data from database to the .NET Application.
Here is the example
Assume you want to fill a grid from database.
1.You will decide what data source are you using,In simple words which server you are going to use.We will take an example of MS-SQL
There are 4 important steps to achieve data from database
1.Connecting to the source.
2.Firing the Query (In .NET Language giving the command)
3.Get the data from query in a Adapter(Adapter Acts as a bridge to get data from database).
4.Store it in a Dataset(it stores the data in dataset to show it in a .NET Control
Note :- You will have to import these 4 namespaces.
using system.data.sqlclient; // this is for connecting to SQL and firing command
using system.data; // this is to store data in dataset.
Suppose on a page load you are getting the data from sql to grid.
protected void Page_Load(object sender, EventArgs e)
// Creating the object of SQLConnection class to connect to SQL
SqlConnection sqlconn = new SqlConnection("Data Source=YourServer;Initial Catalog=YourDatabase;Persist Security Info=True;User ID=Usernameforsqlserver;Password=passwordforsqluser");
sqlconn.Open(); // opening the connection.
SqlCommand cmdins = new SqlCommand("your sql query",sqlconn);
SqlDataAdapter da = new SqlDataAdapter(); // creating the object of sql adapter
da.SelectCommand = new SqlCommand(cmdins); // passing the object of sqlcommand to select command if you are firing select query.
DataSet ds = new DataSet(); // Creating the object of dataset.
da.Fill(ds); // filling the dataset from dataadapter
Gridview1.DataSource = ds; // Now we need to assign the data source to the grid view,so that grid view knows from wher to pick up the data.
Gridview1.DataBind(); // Finally bind the data,if you miss this then the data will not be displayed in Gridview.
}Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator