using System.Data.OleDb;
protected void btn_showst_Click()
{
OleDbConnection con;
try
{
string file = "book";
string fn = "Sheet1";
string fname2 = "[" + fn + "$]";
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\" + file + ".xls;Extended Properties=Excel 8.0;");
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from " + fname2 + "", con);
OleDbDataReader dr;
dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Label2.Text = ex.Message;
}
}
using System.Data.OleDb;
protected void btn_showst_Click()
{
OleDbConnection con;
con = new OleDbConnection("Microsoft.Jet.OLEDB.4.0" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\databasename.mdb");
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from tablename", con);
OleDbDataReader dr;
dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
using System.Data.SqlClient;
protected void btn_showst_Click()
{
SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Database.mdf; Integrated Security=True;User Instance=True");
con.Open();
SqlCommand com = new SqlCommand("select * from " + tablename + " ", con);
SqlDataReader dr = com.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
con.Close();
}
SqlConnection con = new SqlConnection("Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\Database.mdf; Integrated Security=True;User Instance=True");
SqlConnection con = new SqlConnection( "Server=127.0.0.1;Initial Catalog=Database;User ID=sa;Password=123456" );
Thank You
http://hemantrautela.blogspot.com/2012/09/database-connection-in-cnet-aspnet-with.html[
^]