如何以标准的方式编写此代码 [英] How to write this code in standerd way
问题描述
我的代码是
public DataSet Check_Login(string constr)
{
try
{
string strUserName = txtUserName.Text.Trim();
DataSet dsUsers = new DataSet();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand(FetchUserDetailsForLogInId,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ LogInID,strUserName);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsUsers);
con.Close();
返回dsUsers;
}
catch(例外)
{
throw;
}
}
my code is
public DataSet Check_Login(string constr)
{
try
{
string strUserName = txtUserName.Text.Trim();
DataSet dsUsers = new DataSet();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd= new SqlCommand("FetchUserDetailsForLogInId",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@LogInID", strUserName);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dsUsers);
con.Close();
return dsUsers;
}
catch (Exception)
{
throw;
}
}
推荐答案
这不是我们可以回答的问题:做任何事都没有标准方式。好吧,这不是我做的方式,并且使用try ... catch块只是抓住并重新抛出一切都是毫无意义的,但是如果它有效......就可以了。
方法的名称并不反映它所做的工作 - 我希望返回一个DataSet的东西被称为GetUserDetails或类似的东西,我希望一个叫做的方法Check_Login返回OK或Not OK信号,而不是整个用户详细信息。
That isn't a question we can answer: there is no "standard way" of doing anything. OK, it's not the way I'd do it, and using a try...catch block that just catches and re-throws everything is rather pointless, but if it works...it works.
The name of the method doesn't reflect the job it does, either - I'd expect something returning a DataSet to be called "GetUserDetails" or similar, and I'd expect a method called Check_Login to return an "OK" or "Not OK" signal rather than the whole user details.
这篇关于如何以标准的方式编写此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!