asp.net MSSQL Server Connection database

Source: Internet
Author: User
Tags mssql mssql server connectionstrings

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
 %>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.