.NET 中数据库调用的代码片段改进
我可以获得以下代码的问题列表以及修复方法吗?
string ProgramID = HttpContext.Current.Session[CommonFunctions.myNGconnectSessionVars.ProgramId].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyNGConnectDashBoardConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
try
{
cmd.Connection = con;
cmd.CommandTimeout = 900;
cmd.CommandText = "dsb_GetSubscriptionDetailsForSubscriber_ForValidations";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@GPCustomerID", strGPCustomerID);
cmd.Parameters.AddWithValue("@UserName", strUserName);
cmd.Parameters.AddWithValue("@ServerName", ConfigurationManager.AppSettings["ServerName"].ToString());
//code changed by sushma 3/22/2011 as new tab added for reach
//cmd.Parameters.AddWithValue("@ProgramID", CommonFunctions.ProgramID);
cmd.Parameters.AddWithValue("@ProgramID", ProgramID);
cmd.Parameters.AddWithValue("@IsTeacher", blnIsTeacher);
// cmd.CommandTimeout = 0;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return ds;
Can I get a list of issues with the code below, and fixes?
string ProgramID = HttpContext.Current.Session[CommonFunctions.myNGconnectSessionVars.ProgramId].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["MyNGConnectDashBoardConnectionString"].ToString());
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
try
{
cmd.Connection = con;
cmd.CommandTimeout = 900;
cmd.CommandText = "dsb_GetSubscriptionDetailsForSubscriber_ForValidations";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@GPCustomerID", strGPCustomerID);
cmd.Parameters.AddWithValue("@UserName", strUserName);
cmd.Parameters.AddWithValue("@ServerName", ConfigurationManager.AppSettings["ServerName"].ToString());
//code changed by sushma 3/22/2011 as new tab added for reach
//cmd.Parameters.AddWithValue("@ProgramID", CommonFunctions.ProgramID);
cmd.Parameters.AddWithValue("@ProgramID", ProgramID);
cmd.Parameters.AddWithValue("@IsTeacher", blnIsTeacher);
// cmd.CommandTimeout = 0;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return ds;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看到的最大问题是您没有处理一次性对象,例如连接、命令、适配器……因此,如果出现异常,您可能会泄漏资源。要修复您的代码,请将所有一次性资源包装在
using
语句中:我还删除了
try/catch
块,因为您似乎没有在中执行任何有用的操作>catch
语句除了改变堆栈跟踪之外,这是不好的。The biggest issue that I can see is that you are not disposing disposable objects such as connections, commands, adapters, ... so in case of exception you might leak resources. To fix your code wrap all disposable resources in
using
statements:I have also removed the
try/catch
block as you don't seem to be doing anything useful in thecatch
statement other than altering the stack trace which is bad.以下是一些直接改进
使用
using
语句而不是 try/finally 进行连接管理使用
throw
而不是throw ex
重新抛出异常,更好的是,您可以删除 catch 并重新抛出。无需在刚刚实例化的命令上调用
Parameters.Clear
。最后,如果您打算进行大量数据库工作,我建议您考虑使用轻量级 ORM。
Here are some immediate improvements
Use
using
statement rather than try/finally for the connection managementUse
throw
and notthrow ex
to re-throw the exception, better yet you can just remove the catch and re-throwThere is no need to call
Parameters.Clear
on the command that you just instantiated.And finally, if you are going to do a lot of DB work, I would suggest you look at using a lightweight ORM.
除了:
调用数百次时可能会有点慢并且
您是否因为在过去 10 年里一直被强烈劝阻而使用匈牙利表示法而被解雇?
还有:
啊 - 不。 900秒?你认为你到底需要做什么?
是的。就像新对象需要这样。
我解雇了所有使用数据集的人;)除了在通用查询工具中,该工具直接指向用户界面。
不是很多——好吧,而且我们并不真正在这里做作业复习。我宁愿不使用 useles try/catch 而是使用 USING 语句。
Except:
Maybe be a little slow when called hundreds of times and
you are fired for using hungarian notation when strondly discouraged for the last 10 years?
And that:
Ah - no. 900 seconds? What the heck do you think you do there that needs that?
Yeah. Like this is needed on a new object.
And i fire anyone in general who uses datasets ;) Except in a general query tool where that thing geos straight to the UI.
Not a lot - well, also that we dont realyl do homework reviews here. And I rather would not use a useles try/catch instead going to USING statements.
它主要是一个构造,我想评论一下:
throw ex
- 它会改变 ex 的 StackTrace。请改用throw;
。con
和cmd
两个 using 语句其他一些要点:
It's mainly one construct, I want to comment on:
throw ex
- it will alter the StackTrace of ex. Usethrow;
instead.con
andcmd
Some other points: