【问题标题】:Problem with adding SQL paramerters from data access Layer in c#在 c# 中从数据访问层添加 SQL 参数的问题
【发布时间】:2011-10-07 16:59:22
【问题描述】:

首先我给出两种方法的例子

public static List<FormGridEntity> GetAllCandidatesByProgram(string programShortName)
    {
        List<FormGridEntity> formGridEntities = null;
        try
        {
            string cmd = SELECT + " WHERE " + CANDIDATE_PROGRAM_PA + " = " +       CANDIDATE_PROGRAM;`enter code here`
            //string cmd = SELECT

            DAOParameters dps = new DAOParameters();
            dps.AddParameter(CANDIDATE_PROGRAM_PA, programShortName);
            List<SqlParameter> ps = Common.Methods.GetSQLParameters(dps);
            SqlDataReader dataReader = QueryHandler.ExecuteSelectQuery(cmd, ps);
            formGridEntities = Maps(dataReader);
            dataReader.Close();
        }

        catch (Exception exception)
        {
            throw exception;
        }
        return formGridEntities;
    }

另一种方法是

public static List<FormGridEntity> GetAllCandidatesByDates(DateTime initialDate,DateTime finalDate)
    {
        List<FormGridEntity> formGridEntities = null;
        try
        {
            string cmd = SELECT + " WHERE " + FORM_SUBMISSION_DATE_PA + " BETWEEN " + initialDate + " AND " + finalDate;
            //string cmd = SELECT

            DAOParameters dps = new DAOParameters();
            //What will I do here? since 
            **//dps.AddParameter(FORM_SUBMISSION_DATE_PA, initialDate);
            //dps.AddParameter(FORM_SUBMISSION_DATE_PA, finalDate);**
            List<SqlParameter> ps = Common.Methods.GetSQLParameters(dps);
            SqlDataReader dataReader = QueryHandler.ExecuteSelectQuery(cmd, ps);
            formGridEntities = Maps(dataReader);
            dataReader.Close();
        }

        catch (Exception exception)
        {
            throw exception;
        }
        return formGridEntities;
    }

我已经注释掉了

这里的大写术语都是上面类中的常量,比如

private const string FORM_PURCHASING_DATE = "DateOfPurchase";
private const string FORM_PURCHASING_DATE_PA = "@DateOfPurchase"
private const string FORM_SUBMISSION_DATE = "DateOfSubmission";
private const string FORM_SUBMISSION_DATE_PA = "@DateOfSubmission";

SELECT 的定义也正确。我完美地使用了这个层。但是现在发生了一种情况,我对该怎么做感到困惑。到目前为止,我一直在添加具有一对一关系的参数的值。但是,在我的第二种方法中,我使用 between 运算符,其中两个值引用一个参数,我将在这里写什么以使事情正常工作。我已经注释掉了我想要修改的特定区域。如果可能,请帮助我。这是GetSQLParameters方法 公共静态列表 GetSQLParameters(DAOParameters dps) { 列表参数 = new List();

        foreach (DictionaryEntry de in dps.hs)
        {
            SqlParameter p = new SqlParameter();
            p.ParameterName = de.Key.ToString();
            if (de.Value.ToString() == Convert.ToString(0))
            {
                p.Value = DBNull.Value;
            }
            else
            {
                p.Value = de.Value;
            }

            parameters.Add(p);
        }

        return parameters;

【问题讨论】:

  • 我很困惑,一个参数有两个值?你的意思是如果某个值成立,你改变参数值?
  • sql 查询中的 between 运算符没关系,这里使用两个值作为一个列值的边界。
  • 能否展示一下 Common.Methods.GetSQLParameters() 方法的代码?

标签: c# asp.net data-access-layer sql-parametrized-query


【解决方案1】:

在 GetAllCandidatesByDates() 中,也许你应该有:

FORM_SUBMISSION_DATE + "BETWEEN @initialDate AND @finalDate"

然后将initialDate绑定到“@initialDate”参数,将finalDate绑定到“@finalDate”参数?按照现在的方式,您没有使用这些日期的参数,而是将具体的 DateTime 值转换为字符串并将这些字符串直接连接到最终的 SQL 中。

【讨论】:

  • 这就是我一直在寻找的答案! :) 谢谢你的朋友。
猜你喜欢
  • 2011-07-20
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 2012-07-06
  • 1970-01-01
相关资源
最近更新 更多