C#,访问,查询T / F条件并检索其他列字符串

时间:2018-09-10 17:48:56

标签: c# access

晚上

我正在尝试遍历包含品牌名称列表的列表。在每次迭代中,它都会调用查询。查询正在尝试检查以品牌名称命名的给定列是否为true。在这种情况下,请添加第一列的“模型”字符串值。

我能获得的最接近的结果是,它将正确地遍历所有品牌,但会将所有模型添加到每个品牌。从技术上讲,所有型号都对该品牌具有“真实”的意义,但是我不知道一种干净,简单的方法对其进行过滤。

我已经完成了一种测试方法,该方法不是迭代我将其硬编码到查询中的品牌列表中的Parameter值,而是按照预期的方式工作,从而使我不知道为什么它不起作用。参数值在每次迭代中正确更改。

private static void SetAPDictionary()
    {
        if (AP_BRAND_DICTIONARY == null)
        {
            AP_BRAND_DICTIONARY = new Dictionary<string, List<string>>();

            // For each Brand added into the Brand Catalog, iterate through and query for each brand and allocate accordingly into the Dictionary. 
            foreach (string brand in Root_Toolbox.BrandCatalog)
            {
                Console.WriteLine("NOW QUERYING FOR "+ brand);
                //DemoCode(brand);
                APQueryCaller(brand);
            }
        }
    }

    // TEST CODE; WORKS AS DESIRED.
    private static void DemoCode(string brand)
    {
        using (OleDbCommand cmd = new OleDbCommand(@"SELECT [Model] FROM [Copy of AP_Brand] WHERE @p1 = -1", Connection_Controller.MasterDbConnection(true)))
        {
            cmd.Parameters.AddWithValue("@p1", brand);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine("DEMO ADDING " + reader.GetString(0) + " TO " + brand);
            }
        }
        Connection_Controller.MasterDbConnection(false);
    }
    // END TEST

    // TODO: FIX ME... 
    public static void APQueryCaller(string brand)
    {
        using (OleDbCommand cmd = new OleDbCommand(@"SELECT [Model] FROM [AP_Brand] WHERE @p1 = YES", Connection_Controller.MasterDbConnection(true)))
        {
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@p1", brand);

            // DEBUG 
            Console.WriteLine(cmd.CommandText);
            foreach (OleDbParameter i in cmd.Parameters)
            {
                Console.WriteLine("@p1 IS NOW: " + i.Value);
            }
            // END DEBUG


            OleDbDataReader reader = cmd.ExecuteReader();

            if (!AP_BRAND_DICTIONARY.ContainsKey(brand))
            {
                Console.WriteLine("ADDING " + brand);
                AP_BRAND_DICTIONARY.Add(brand, new List<string>());
            }

            while (reader.Read())
            {
                // If the column with the brand name is checked (true) add to the dictionary. 

            }
        }
        Connection_Controller.MasterDbConnection(false);
    }

关于数据库布局。 MDU应该是将其型号列入清单的唯一品牌。

Database Table

1 个答案:

答案 0 :(得分:1)

就像Igor在他的评论中指出的那样……您未在where子句中使用列名。我无法发表评论(信誉尚不足)。

在我看来,品牌是一个字符串值吗?例如:“ MDU”

这是您现在拥有的:

  SELECT [Model] FROM [AP_Brand] WHERE @p1 = YES
  --Are you expecting this to turn into
  SELECT [Model] FROM [AP_Brand] WHERE MDU = YES

我的建议:

@"SELECT [Model] FROM [AP_Brand] WHERE " + brand + " = @p1";

cmd.Parameters.AddWithValue("@p1", -1);

现在输出:

SELECT [Model] FROM [AP_Brand] WHERE MDU = -1

注意: 连接上面的字符串已打开以进行SQL注入,请确保在构建SQL语句之前先清除brand变量。