Null参数不能与Access和OLE一起使用

时间:2014-10-03 11:40:25

标签: c# sql ms-access dbnull

我的理解是使用dbnull.value传递一个null via ole参数。

这是我的代码

// Add user control to panel
pcMaster.Controls.Add(new ucIndexCodes(DBNull.Value, DBNull.Value));

ucIndexCodes控件在

中有此功能
public ucIndexCodes(object CustomerID, object ProjectID)
{
    InitializeComponent();
    try
    {
        // Build parameters
        Queue<OleDbParameter> myParameters = new Queue<OleDbParameter>();

        myParameters.Enqueue(new OleDbParameter("@CustomerID", CustomerID));
        myParameters.Enqueue(new OleDbParameter("@ProjectID", ProjectID));

        // Build SQL statement
        string mySQL = "SELECT IndexCodeID, Attitude, Description, recursiveIndexCodeID, CustomerID, ProjectID FROM tblIndexCodes WHERE CustomerID = @CustomerID AND ProjectID = @ProjectID";
        // Get Data
        using (DataTable tblTopIndexCodes = Classes.clsDatabase.GetData(mySQL, myParameters))
        {
            if (tblTopIndexCodes != null)
            {
                foreach (DataRow tmpRow in tblTopIndexCodes.Rows)
                {
                    // Add Root node
                    TreeListNode objRoot = tlIndexCodes.Nodes.Add(new object[] { tmpRow["Attitude"], tmpRow["Description"].ToString() });
                    // Add child node of root
                    AddChildNode(objRoot, Convert.ToInt16(tmpRow["IndexCodeID"]));
                }
            }
        }
    }
    catch (Exception ex)
    {
        ErrorHandle(ex);
    }
}

问题是GetData返回一个空表,所以没有任何行表示它正确运行只是没有返回任何内容。

internal static DataTable GetData(string mySQL, Queue<OleDbParameter> myParameters = null)
{
    // Data set to return
    DataTable myTable = new DataTable();
    try
    {
        using (OleDbConnection myConn = new OleDbConnection())
        {
            // Open connection to database
            myConn.ConnectionString = getStringConnection();
            myConn.Open();

            // Build SQL command
            using (OleDbCommand myCMD = new OleDbCommand())
            {
                myCMD.CommandText = mySQL;
                myCMD.CommandTimeout = 15000;
                myCMD.CommandType = System.Data.CommandType.Text;
                myCMD.Connection = myConn;
                // Add parameters
                if (myParameters != null)
                {
                    foreach (OleDbParameter myParameter in myParameters)
                    {
                        myCMD.Parameters.Add(myParameter);
                    }
                }

                using (OleDbDataAdapter myData = new OleDbDataAdapter())
                {
                    myData.SelectCommand = myCMD;
                    myData.Fill(myTable);
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.Write(ex.Message.ToString());
        myTable = null;
    }
    return myTable;
}

当我在Access中运行此SQL时,它返回预期的数据。

SELECT IndexCodeID, Attitude, Description, recursiveIndexCodeID, CustomerID, ProjectID FROM tblIndexCodes WHERE CustomerID Is Null AND ProjectID Is Null

CustomerID和/或ProjectID可能并不总是为空,因此对于像这样的查询,我需要相同的信号。

SELECT IndexCodeID, Attitude, Description, recursiveIndexCodeID, CustomerID, ProjectID FROM tblIndexCodes WHERE CustomerID = 1 AND ProjectID = 1

SELECT IndexCodeID, Attitude, Description, recursiveIndexCodeID, CustomerID, ProjectID FROM tblIndexCodes WHERE CustomerID = 1 AND ProjectID = Null

我无法找到解决方案。我确保CustomerID和ProjectID实际上是null,方法是将默认值设置为null,并将customerid和projectid更新为null,仅用于测试。

感谢您的帮助, 吉姆

2 个答案:

答案 0 :(得分:3)

NULL的含义是&#39;未知价值&#39;但NULL不是一个值,因此无法按值处理;要比较和评估NULL,您必须使用特定说明。

您的查询可以重写为:

SELECT IndexCodeID, Attitude, Description, recursiveIndexCodeID, CustomerID, ProjectID
FROM tblIndexCodes
WHERE 
  (
    (@CustomerID is not null and CustomerID = @CustomerID)
    or
    (@CustomerID is null and CustomerID is null)
  )
  AND
  (
    (@ProjectID is not null and ProjectID = @ProjectID)
    or
    (@ProjectID is null and ProjectID is null)
  )

答案 1 :(得分:1)

在SQL中,与NULL的比较总是计算为false,即使是NULL = NULL。

您可以通过修改SQL来修复它:

       string mySQL = "SELECT IndexCodeID, Attitude, Description, recursiveIndexCodeID, CustomerID, ProjectID FROM tblIndexCodes WHERE (@CustomerID IS NULL OR CustomerID = @CustomerID) AND (@ProjectID IS NULL OR ProjectID = @ProjectID)";