c# execute SqlCommand with Parameters in “using” code block

不问归期 提交于 2019-12-29 09:02:06

问题


I am attempting to execute an SQL Command through a 'using' code block, but can't seem to get it to work with Parameters. I get the error: 'Parameters does not exist in the current context', does anyone have a possible solution for this problem? Heres My code:

DataTable dt = new DataTable();
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
using (var cmd = new SqlCommand(" SELECT FName" +
                                        " FROM EmployeeTable " +
                                        " WHERE EmployeeId = @empId",
                                        con)
                                        {
                                            Parameters.Add(new SqlParameter("@empId",empId))
                                        })
        {
            try
            {
                   con.open();
                   dt.Load(cmd.ExecuteReader());
            }
            catch(Exception ex)
            {
                 //(snip) Log Exceptions
            }
        }
        return dt;

回答1:


Don't use constructor initialization for this.

DataTable dt = new DataTable();
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
using (var cmd = new SqlCommand(" SELECT FName" +
                                        " FROM EmployeeTable " +
                                        " WHERE EmployeeId = @empId",
                                        con))
{
    cmd.Parameters.Add(new SqlParameter("@empId",empId));
    try
    {
           con.open();
           dt.Load(cmd.ExecuteReader());
    }
    catch(Exception) //BAD BAD BAD!!! Why are you doing this!
    {
    }
}
return dt;

Also why are you catching all exceptions and throwing them away, this is a horrible thing to. If you have a exception you think is going to be common first see if you can refactor your code to check the for the input values that would cause it and not have it thrown at all (perhaps even throw an ArgumentException of your own), and if you can't do that then check for that specific exception, not every possible one.




回答2:


It is impossible to use object initializer for anything other than property assignment, so you should rewrite your code like this (only relevant part here):

...
using (var cmd = new SqlCommand(" SELECT FName" +
                                        " FROM EmployeeTable " +
                                        " WHERE EmployeeId = @empId",
                                        con))
{
    cmd.Parameters.Add(new SqlParameter("@empId", empId));
...



回答3:


Your second using statement closes before the try catch. Therefore once the try catch is reached the using block has already been closed. The parameters are therefore in the incorrect scope.



来源:https://stackoverflow.com/questions/18316767/c-sharp-execute-sqlcommand-with-parameters-in-using-code-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!