ASP tutorial. NET MSSQL Server Connection Database tutorial
*/
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Btn_sendmessage_click (object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection (@ "sever =. Sqlexpress;database=guestbook; trusted_connection=true");
String sql = "INSERT into Tbguestbook (username,posttime,message,isreplied,reply) VALUES (' Test ', ' 2006-11-1 12:00 ', ' Www.111cn.net ', 0, ' AC ');
SqlCommand cmd = new SqlCommand (SQL, conn);
Conn.Open ();
Cmd.executenonquery ();
Conn.close ();
}
Querying data after connecting to a database
private void Page_Load (object sender, System.EventArgs e)
{
if (!ispostback)
{
String myconnectstring = "Server=localhost;uid=sa;database=manager";
SqlConnection myconnection =new SqlConnection (myconnectstring);
String comstring = "SELECT * from Newstudent";
SqlCommand mycommand = new SqlCommand (comstring,myconnection);
Mycommand.connection=myconnection;
Myconnection.open ();
DataSet ds=new DataSet ();
SqlDataAdapter Mydataadepter =new SqlDataAdapter (comstring,myconnection);
Mydataadepter.fill (ds, "newstudent");
datagrid1.datasource=ds.tables["Newstudent"].defaultview;
Datagrid1.databind ();
datalist.datasource=ds.tables["Newstudent"].defaultview;
Datalist.databind ();
}
}
Method Three
Can be set in the Web.config file in VS,
<connectionstrings>
<add name= "sqlConnectionString" ConnectionString = "Data source=pc-200909160824; Initial catalog=shopping; Integrated security=true "></add>
</connectionstrings>
Data source is the connected data sources, initial Catalog is the name of the database you want to connect to, integrated security explains how you connect to the database for Windows Authentication
%>