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;
}