SQL database operations in ASP. NET (configure and obtain connection strings)

Source: Internet
Author: User

Configure the database connection string in WebConfig. The Code is as follows: Copy codeThe Code is as follows: <connectionStrings>
<Add name = "ConnectionString" connectionString = "user id = username; password = password; initial catalog = database name; data source = server name"/>
</ConnectionStrings>

Then obtain the connection string in Webform_1.aspx.cs and add the following reference;Copy codeThe Code is as follows: using System. Configuration;
Using System. Data;
Using System. Data. SqlClient;

Code:Copy codeThe Code is as follows: SqlConnection con;
Protected void Page_Load (object sender, EventArgs e)
{
ConnectDB ();
}
Private void ConnectDB ()
{
String ConString = ConfigurationManager. ConnectionStrings ["ConnectionString"]. ConnectionString;
Con = new SqlConnection (ConString );
Con. Open ();
SqlCommand com = new SqlCommand ();
SqlDataReader sdr;
String sqlstr = "select * from item ";
Com. CommandText = sqlstr;
Com. Connection = con;
Sdr = com. ExecuteReader ();
While (sdr. Read ())
{
Response. Write (sdr ["field name"]. ToString () + "</br> ");
}
Sdr. Close ();
Sdr = null;
}

This is enough. It's very simple. I hope my friends will give me more advice and appreciate it!

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.