In asp.net webform projects we create a connection channel where ever we need to access(insert,update,delete,select) the database which is like more or less as below mentioned.
My thought about this is can we create a class which stores all the queries and the connection string.is this way is correct ? if not what is the professional way ?
public bool ValidateFunc(string findText, string findField)//checking whether the mailid or username already exits
{
.. ..
.. ..
string connstr = ConfigurationManager.ConnectionStrings["Connectionstr"].ConnectionString;
SqlConnection Connection = new SqlConnection(connstr);
string seletquery = @"SELECT * FROM Registration WHERE" + findField + " = @Findt";
SqlCommand Command = new SqlCommand(seletquery, Connection);
Command.Parameters.AddWithValue("@Findt", findText);
Connection.Open();
SqlDataReader ad = Command.ExecuteReader();
.. ..
.. ..
}
public void InsertFunc()//inserting into database
{
string connstr = ConfigurationManager.ConnectionStrings["Connectionstr"].ConnectionString;
SqlConnection Connection = new SqlConnection(connstr);
string insertq = @"insert into Registration values (@fname,@lname,@dob,@emailid,@uname)";
SqlCommand Command = new SqlCommand(insertq, Connection);
Connection.Open();
Command.ExecuteNonQuery();
Connection.Close();
}