ASP. NET Connection Database

Source: Internet
Author: User

ASP. NET Web Connection database step.

First, create a new Web project.

1. Add new Web site->asp.net Web site WinForm form.

2. The new website has a Web. config file at the bottom and a database connection string in a file, as follows:

<connectionStrings>

<add name= "DefaultConnection" connectionstring= "

Data source=pc-20160403skqw\sqlexpress;initial catalog=cad;uid=sa;pwd=123456;integrated Security=True " Providername= "System.Data.SqlClient"/>

</connectionStrings>

3. Where data Source is the database instance name, if it is a network database, data source=ip, port; For example, Data source= 192.1608.68.12,2000;

If it is a local user, the data source= database instance name, such as data source= pc-20160403skqw\sqlexpress, can be seen when logging into the data database, such as the name of the instance:

4. Initial catalog= database name; uid= login account; pwd= password; Integrated security=true

Second, the code implementation read.

Create a new WinForm form test.aspx, write code in Test.aspx.cs

Using System.Configuration;

Using System.Data.SqlClient;

protected void Page_Load (object sender, EventArgs e)

{

1. Get the database connection string that is configured in the configuration file

String connectionString = configurationmanager.connectionstrings["DefaultConnection"]. ToString ();

SqlConnection connection =new SqlConnection (connectionString);

Create the Command and Parameter objects.

2.sql statements

String queryString =

"SELECT * from Solution";

SqlCommand command = new SqlCommand (queryString, connection);

Open the connection in a Try/catch block.

Create and execute the DataReader, writing the result

Set to the console window.

3. Open the Connection

Connection. Open ();

4. Execute SQL statements

SqlDataReader reader = command. ExecuteReader ();

5. View the returned results

while (reader. Read ())

{

Console.WriteLine ("\t{0}\t{1}\t{2}",

Reader[0], reader[1], reader[2]);

String a = reader[0]. ToString ();

Console.WriteLine (a);

}

6. Close the connection

Reader. Close ();

}

ASP. NET Connection Database

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.