在 asp.net webform 项目中,我们创建了一个连接通道,我们需要访问(插入、更新、删除、选择)数据库,这或多或少如下所述。
我的想法是我们可以创建一个存储所有查询和连接字符串的类。这种方式是否正确?如果不是专业的方法是什么?
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();
}