【问题标题】:Make the method parameterized使方法参数化
【发布时间】:2014-12-06 13:13:42
【问题描述】:

我以分层的方式设计了我的应用程序。每个接口都有一个 BusinessOP 层和一个通用数据访问层。在我的数据访问层中,我有这样的数据读取器方法。|

     public SqlDataReader executeQuerys(string query01)
    {
        SqlConnection con = null;
        SqlCommand com = null;
        try
        {
            con = new SqlConnection(DBConnect.makeConnection());
            con.Open();
            com = new SqlCommand(query01, con);
            return com.ExecuteReader(CommandBehavior.CloseConnection);
        }

        catch
        {
            com.Dispose();
            con.Close();
            throw;
        }

这是我的 DBConnection 层的代码。

     public static string makeConnection()
    {
        string con = ConfigurationManager.ConnectionStrings["MyDB.Properties.Settings.ConString"].ToString();
        return con;
    }

在我的业务层中,我有这样的方法,每个方法都调用一个特定的存储过程。

     public SqlDataReader getLGDivID(string divName)
    {
        string query = "EXEC getLGDivID'" + divName + "'";
        return new DataAccessLayer().executeQuerys(query);
    }

由于我的业务操作层是不安全的,我想在这里使用参数化查询,我使用字符串连接来传递参数。谁能提示我如何修改它?

【问题讨论】:

  • 旁注:您应该始终.Dispose,而不仅仅是在捕获异常时。

标签: c# sql-server data-access-layer business-logic-layer parameterized-query


【解决方案1】:

你可以稍微改变你的功能:

public SqlDataReader executeQuerys(string query01, string paramName, string value)
{
    SqlConnection con = null;
    SqlCommand com = null;
    try
    {
        con = new SqlConnection(DBConnect.makeConnection());
        con.Open();
        com = new SqlCommand(query01, con);
        com.Parameters.AddWithValue(paramName, value);
        com.Dispose();
        con.Close();
    }
    catch
    {
        com.Dispose();
        con.Close();
        throw;
    }
   return com.ExecuteReader(CommandBehavior.CloseConnection);
}

然后使用它:

public SqlDataReader getLGDivID(string divName)
{
    string query = "EXEC getLGDivID @divName";
    return new DataAccessLayer().executeQuerys(query, "@divName", divName);
}

编辑:

正如@silvermind 指出的那样,您应该正确处理您的连接。 您现在拥有它的方式只有在您捕获异常时才会释放连接。

这样不好,用IDisposable,例如:

public SqlDataReader executeQuerys(string query01, string paramName, string value)
{
    using (SqlConnection con = new SqlConnection(DBConnect.makeConnection()))
    {
        try
        {
            con.Open();
            com = new SqlCommand(query01, con);
            com.Parameters.AddWithValue(paramName, value);
        }
        catch(SqlException ex)
        {
            //Handle the exceptio
            //no need to dispose connection manually
            //using statement will take care of that
        }
    }
    return com.ExecuteReader(CommandBehavior.CloseConnection);
}

【讨论】:

  • 您也许可以考虑使用 using 块显示适当的处理以改进答案。 :)
  • @meda 它给了我这个错误,Error DataAccessLayer.executeQuerys(string, string, string)': 不是所有的代码路径都返回一个值
  • @chathwind 有道理,检查我的编辑,后一个函数
  • @meda 当我尝试以这样的表单代码读取数据读取器中的值时,它 SqlDataReader reader = new CandidateOP().getLGDivID(cmbLgDiv.Text); if (reader.HasRows) { while (reader.Read()) { templgDivID = (Int32)reader[0]; } 读者。关闭();它给了我错误 Invalid attempt to call HasRows 当阅读器关闭时,但它适用于我以前的代码。你认为原因是什么..?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-31
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
相关资源
最近更新 更多