To configure the database connection string in Webconfig, the code is as follows:
Copy Code code as follows:
<connectionStrings>
<add name= "ConnectionString" connectionstring= "user id= username; password= password; initial catalog= database name; Data source= server name"/ >
</connectionStrings>
Then get the connection string inside the Webform_1.aspx.cs to add the following reference;
Copy Code code as follows:
Using System.Configuration;
Using System.Data;
Using System.Data.SqlClient;
Code:
Copy Code code 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 can be, very simple, beginner asp.net, I hope friends more advice, grateful!