Read data from Sql server 2005 Database
Step 1) Install and configure Sqlserver 2005
Step 2)Write following code to fetch data from database
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class sqlserver :
System.Web.UI.Page
{
SqlConnection con = new
SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader rd;
public string query;
protected void
connection()
{
con.ConnectionString = "server=.\\SQLEXPRESS;
integrated security=SSPI; initial
catalog=sourav;";
//Initial catalog =sourav is my database name.
con.Open();
cmd.Connection
= con;
}
protected void
Page_Load(object sender, EventArgs e)
{
//Calling connection function
connection();
cmd.CommandText
= "select * from tablename";
// Change table name according to your
setting
rd =
cmd.ExecuteReader();
if (rd.Read())
{
Response.Write(rd.GetSqlValue(0).ToString());
Response.Write(rd.GetSqlValue(1).ToString());
}
rd.Close();
}
}
N.B:- don’t forget to change server name , database name and
table name according to your setting.
No comments:
Post a Comment