如何调试 ExecuteNonQuery 上的空对象字段值?
How to debug null object field value on ExecuteNonQuery?
我已将包含多个字段值的类型化对象传递给调用 Insert 存储过程的 SQL 命令。在调试时我可以看到每个字段都有一个值并且 none 为空。
但是当我进一步调试 db.ExecuteNonQuery
调用时,我得到一个 SQL 异常,指出 Application 字段为空 。虽然它不为空并且在执行此命令之前具有字符串值。
我检查了通常的调试步骤,模型字段类型与 DbCommand
中的类型匹配。我还在执行查询之前检查了 escalation.Application
值,该查询按预期填充了字符串值。
问题:
有谁知道为什么字段值在 ExecuteNonQuery
上被评估为 null?
代码:
DAL class -
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>
通常,数据库参数必须以@开头。因此,如果您为此更改代码:
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);
应该可以。
我已将包含多个字段值的类型化对象传递给调用 Insert 存储过程的 SQL 命令。在调试时我可以看到每个字段都有一个值并且 none 为空。
但是当我进一步调试 db.ExecuteNonQuery
调用时,我得到一个 SQL 异常,指出 Application 字段为空 。虽然它不为空并且在执行此命令之前具有字符串值。
我检查了通常的调试步骤,模型字段类型与 DbCommand
中的类型匹配。我还在执行查询之前检查了 escalation.Application
值,该查询按预期填充了字符串值。
问题:
有谁知道为什么字段值在 ExecuteNonQuery
上被评估为 null?
代码:
DAL class -
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>
通常,数据库参数必须以@开头。因此,如果您为此更改代码:
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);
应该可以。