1. Add a connection string in your application's web.config file.
In the connectionStrings tag, add a key and value for publishing the connection.
2.Include the following namespaces.
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
3.Declare the basic variables for database interaction in your application.
SqlConnection con;
SqlCommand cmd;
SqlDataReader rd;
4.Establish connection with your database.
con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString());
con.Open();
5.After establishing connection. It is possible perform all the operations on the database. First we look into entering data into the database.
cmd = new SqlCommand("insert into [tablename] values(@arg1,@arg2,...)",con);
cmd.Parameters.Add("@rangeFrom", SqlDbType.Int).Value = [yourvalue];
cmd.Parameters.Add("@rangeTo", SqlDbType.VarChar).Value = [yourvalue];
cmd.ExecuteNonQuery(); // this executes the above query and the data is stored.
6.So we stored the data. Now let us focus on retrieving the data.
cmd = new SqlCommand("select * from [tablename]", con);
rd = cmd.ExecuteReader(); // used for select statements.
Grid.DataSource = rd; // Assigning the data to a GridView Control
Grid.DataBind();
7. Once you run your application, the data from the table will displayed in the Grid.
No comments:
Post a Comment