Skip to main content
 首页 » 编程设计

c#之Null 参数不适用于 Access 和 OLE

2024年02月06日30www_RR

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

这是我的代码

// 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 可能并不总是 null,因此我需要等同的信号来进行此类查询。

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 

我找不到解决这个问题的方法。我通过将默认值设置为 null 并将 customerid 和 projectid 更新为 null 来确保 CustomerID 和 ProjectID 实际上为 null 以进行测试。

感谢您的帮助, 吉姆

请您参考如下方法:

NULL 的含义是“未知值”,但 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) 
  )