如何在ExecuteNonQuery上调试null对象字段值?

时间:2016-05-11 20:22:12

标签: c# sql sql-server mode executenonquery

我已经将包含多个字段值的类型化对象传递给SQL命令,该命令调用 Insert 存储过程。在调试时,我可以看到每个字段都有一个值,没有一个是空的。

但是当我进一步调试db.ExecuteNonQuery调用时,我得到一个SQL异常,指出 Application字段为空。虽然它不是null并且在执行此命令之前具有字符串值。

我检查了通常的调试步骤,模型字段类型与DbCommand中的类型匹配。此外,我在执行查询之前检查了escalation.Application值,该查询填充了预期的字符串值。

问题:

有谁知道为什么在ExecuteNonQuery上将字段值评估为空?

代码:

  • DAL类 -

    public bool InsertWebReq(Escalation escalation)
    {
        Database db = null;
        string sqlCommand = "";
    
        try
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory();
            db = factory.Create("NOTIFICATION");
    
            sqlCommand = "CREATE_ESCALATION";
    
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
            {
                db.AddInParameter(dbCommand, "Application", DbType.String, escalation.Application);
                db.AddInParameter(dbCommand, "EM", DbType.String, escalation.EM);
                db.AddInParameter(dbCommand, "EscalationActions", DbType.String, escalation.EscalationActions);
                db.AddInParameter(dbCommand, "ProblemStatement", DbType.String, escalation.ProblemStatement);
                db.AddInParameter(dbCommand, "status", DbType.String, escalation.status);
                db.AddInParameter(dbCommand, "UpdatedBy", DbType.String, escalation.UpdatedBy);
                db.AddInParameter(dbCommand, "UpdatedTime", DbType.DateTime, escalation.UpdatedTime);
                db.AddInParameter(dbCommand, "Impact", DbType.String, escalation.Impact);
    
                db.ExecuteNonQuery(dbCommand);
                return true;
            }
        }
        catch (SqlException ex)
        {
            //Log the SQL specific errors
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                StringBuilder errorMessages = new StringBuilder();
    
                errorMessages.Append("Index #" + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Procedure: " + ex.Errors[i].Procedure + "\n");
            }
    
            MyLogger.FileLogger.ErrorFormat("{0} - {1}", sqlCommand, ex.Message);
            return false;
        }
    }
    
  • 模型 -

    public partial class Escalation
    {
        public int ID { get; set; }
        public string Application { get; set; }
        public string EM { get; set; }
        public string status { get; set; }
        public string ProblemStatement { get; set; }
        public string Impact { get; set; }
        public string EscalationActions { get; set; }
        public System.DateTime UpdatedTime { get; set; }
        public string UpdatedBy { get; set; }
    }
    
  • 应用程序列表的Html元素 -

                            <div class="form-adjacent">
                                <label class="col-md-3 control-label" for="App">App</label>
                                <div class="col-md-8">
                                    <select id="App" name="Application" onchange="" class="form-control">
                                        <option value="SAP">SAP</option>
                                        <option value="EME">EME</option>
                                    </select>
                                </div>
                            </div>
    

1 个答案:

答案 0 :(得分:1)

通常,数据库参数必须以@开头。因此,如果您为此更改代码:

db.AddInParameter(dbCommand, "@Application", DbType.String, escalation.Application);
db.AddInParameter(dbCommand, "@EM", DbType.String, escalation.EM);
db.AddInParameter(dbCommand, "@EscalationActions", DbType.String, escalation.EscalationActions);
db.AddInParameter(dbCommand, "@ProblemStatement", DbType.String, escalation.ProblemStatement);
db.AddInParameter(dbCommand, "@status", DbType.String, escalation.status);
db.AddInParameter(dbCommand, "@UpdatedBy", DbType.String, escalation.UpdatedBy);
db.AddInParameter(dbCommand, "@UpdatedTime", DbType.DateTime, escalation.UpdatedTime);
db.AddInParameter(dbCommand, "@Impact", DbType.String, escalation.Impact);

它应该有用。