【问题标题】:NullReferenceException when Adding a SqlParameter to a SqlParameterCollection将 SqlParameter 添加到 SqlParameterCollection 时出现 NullReferenceException
【发布时间】:2015-12-04 06:00:18
【问题描述】:

为了解决这个问题,我已经绞尽脑汁好几个小时了,希望有人能帮我解决这个问题。

我在 c# 应用程序中插入数据库。作为一名优秀的编码员,我正在参数化我的查询。

相关代码为:

int contactId = -1;

oString = "INSERT into {0}.Creditors (AccountID, DissectionId, Reference, FileAs, ABN, Created, Modified, GUID)"
    + " output INSERTED.ID"
    + " values (1, @dissectionId, ";
if(disbursement.trade_supplier.trust_id.Length > 0)
{
    oString += "@reference, ";
}
else
{
    oString += "null, ";
}
if(disbursement.trade_supplier.trading_name.Length > 0)
{
    oString += "@name, ";
}
else
{
    oString += "null, ";
}
if(disbursement.trade_supplier.business_number.Length > 0)
{
    oString += "@abn, ";
}
else
{
    oString += "null, ";
}
oString += " CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, @guid)";
oString = string.Format(oString, "[" + textBox1.Text + "].[dbo]");
using (SqlCommand insertCreditor = new SqlCommand(oString))
{
    insertCreditor.Connection = myConnection;
    insertCreditor.Parameters.Add("@dissectionId", SqlDbType.Int).Value = Convert.ToInt32(defaultDissectionID);
    if (disbursement.trade_supplier.trust_id.Length > 0)
    {
        insertCreditor.Parameters.Add("@reference", SqlDbType.NVarChar, 8).Value = disbursement.trade_supplier.trust_id;
    }
    if (disbursement.trade_supplier.trading_name.Length > 0)
    {
        insertCreditor.Parameters.Add("@name", SqlDbType.NVarChar, 200).Value = disbursement.trade_supplier.trading_name;
    }
    if (disbursement.trade_supplier.business_number.Length > 0)
    {
        SqlParameter abn = new SqlParameter("@abn", SqlDbType.NVarChar, 14);
        abn.Value = disbursement.trade_supplier.business_number;
        MessageBox.Show(abn.GetType().ToString());
        MessageBox.Show(abn.Value.ToString());
        if (insertCreditor == null)
        {
            MessageBox.Show("InsertCreditor is null");
        }
        if (insertCreditor.Parameters == null)
        {
            MessageBox.Show("Parameters is null");
        }
        if(abn == null)
        {
            MessageBox.Show("null object sql parameter");
        }
        else
        {
            MessageBox.Show("sql parameter is not null");
            if (insertCreditor.Parameters == null)
            {
                MessageBox.Show("Parameters collection is null?");
            }
            else
            {
                MessageBox.Show("Parameters collection is not null");
                insertCreditor.Parameters.Add(abn);
            }
        }
    }
    if (disbursement.trade_supplier.guid.Length > 0)
    {
        insertCreditor.Parameters.Add("@guid", SqlDbType.UniqueIdentifier).Value = disbursement.trade_supplier.guid;
    }
    else
    {
        insertCreditor.Parameters.Add("@guid", SqlDbType.UniqueIdentifier).Value = new Guid();
    }
}

违规行是insertCreditor.Parameters.Add(abn);

这会导致 NullReferenceExecption: Object Reference not set to an Object 的实例。

但是,我的 MessageBox 调用显示 abninsertCreditor.Parameters 都不是 null。

如果我删除此部分并在那里只使用 null,它会通过它,但在此之后的下一个插入中会再次发生不同的值。

当我在违规行断点时显示的变量:

abn {@abn}  System.Data.SqlClient.SqlParameter
abn.Value   "1223334555"    object {string}
disbursement.trade_supplier {PSConsoleExtractor.TradeSupplier}  PSConsoleExtractor.TradeSupplier
disbursement.trade_supplier.business_number "1223334555"    string
insertCreditor.Parameters   {System.Data.SqlClient.SqlParameterCollection}  System.Data.SqlClient.SqlParameterCollection
this    {PSConsoleExtractor.Form1, Text: PropertySafe Console Exporter (v 2.0.2.0)} PSConsoleExtractor.Form1

有什么想法吗?

【问题讨论】:

  • 但它并没有说insertCreditor.Parametersnull。它说它不是一个对象。这是巨大的差异。检查它真正包含什么,例如使用调试器。
  • 使用消息框是一种非常奇怪的调试方式。顺便说一句,如果你将if { } if { } 块更改为if { } else if { } 块会怎样?
  • 我使用 MessageBoxes,因为我需要运行命令行参数,但我不确定如何使用调试器执行此操作,但是如果我将它们更改为 else if 语句而不是嵌套 if 语句,我会相同的最后一点并得到相同的错误。如果有人可以解释如何使用命令行参数进行调试,我很乐意尝试。
  • @DawidFerenczy 你是什么意思?错误确实表示某些内容为空。
  • 好的,使用命令行参数进行调试,insertCreditor.Parameters 的类型为 System.Data.SqlClient.SqlParameterCollection,abn 的类型为 System.Data.SqlClient.SqlParameter。然而,尽管在“abn.Value = disbursement_trade_supplier.business_number;”行上添加了一个断点,它还是跳过了那个断点。但是,如果我使用调试器向下钻取足够远,则该集合中确实出现了第 4 个参数,该参数为空。这正常吗?

标签: c# sql-server nullreferenceexception sqlparameter


【解决方案1】:

我最终通过添加一些额外的空检查解决了这个问题,这样如果它为空,它就无法到达出错的地步。这对我来说仍然没有意义,因为我检查了变量的每个部分并且没有一个为空,但它以某种方式工作。

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多