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