For usability, and ease of editing, store your connection settings to DB in WebConfig of the site like such:
<connectionStrings>
<add name="customNameForConnection" connectionString="Data Source=nameOfServer or IP;Initial Catalog=nameOfTable;Persist Security Info=True;User ID=Login;Password=Password" />
</connectionStrings>
using System.Data.SqlClient;
lang="c#">public partial class ClassFile: System.Web.UI.Page
{
SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["customCon"].ConnectionString);
using(con){
con.Open();
string commandText = "SELECT * from DB";
using (SqlCommand command = new SqlCommand(commandText, con))
{
using (SqlDataReader dataReader = command.ExecuteReader())
{
if(dataReader.Read())
{
}
}
}
}
}
if you have a DB query with conditions, you can use commandParameters to utilise changing variables in the commandString variable like so:
var commandString = "SELECT * FROM DB where b = @id";
and then in then inside the usingSqlCommand brackets you would add the code:
command.Parameters.Add("@id", SqlDbType.Int).Value = nameOfVariable;
Note you can have multiple commandParameters.
hope this gives you help you need in connecting to SQL