[Npgsql.PostgresException,尝试在C#Winform中查询PostgreSQL数据库时出错

问题描述 投票:0回答:1

我正在尝试从数据库查询中获取字符串列表,并将其放入ComboBox中。

我正在尝试执行的代码从DataGridVew单元格Click事件中的以下内容开始:

// Bind combobox to dgv and than add list of New Values to combobox
DataGridViewComboBoxCell cboNewValueList = new DataGridViewComboBoxCell();

// Get fields to build New Value query
List<string> lsNewValuesResult = new List<string>();
string strCategory = dtCategories.Rows[e.RowIndex][1].ToString();
string strCompanyName = cboSelectCompany.Text;
string strQueryGetNewValuesValidationInfo = "SELECT validationdb, validationtable, validationfield, validationfield2, validationvalue2" +
                                        " FROM masterfiles.categories" +
                                        " WHERE category = @category";

// Pass validation info query to db class and return list of New Values
db getListOfNewValues = new db();
lsNewValuesResult = getListOfNewValues.GetNewValuesList(strQueryGetNewValuesValidationInfo, strCategory, strCompanyName);

//Populate the combobox with the list of New Values
foreach (string strListItem in lsNewValuesResult)
{
    cboNewValueList.Items.Add(strListItem);
}

我需要使用上面的查询来找出查询填充lsNewValuesResult所需的表和字段。

GetNewValuesList中,我有此代码:

public List<string> GetNewValuesList(string strValidationInfoQuery, string strCategory, string strCompanyName)
{
    List<string> lsValidationInfo = new List<string>();
    List<string> lsNewValuesList = new List<string>();

    using (NpgsqlConnection conn = new NpgsqlConnection(connString))
    using (NpgsqlCommand cmd = new NpgsqlCommand(strValidationInfoQuery, conn))
    {
        cmd.Parameters.Add(new NpgsqlParameter("@cateogry", strCategory));
        conn.Open();

        using (NpgsqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                int intReaderIndex;
                for (intReaderIndex = 0; intReaderIndex <= reader.FieldCount - 1; intReaderIndex++)
                {
                    // reader indexes 3 & 4 correspond to categories.validationfield2 and validationvalue2, which can be null
                    if (string.IsNullOrEmpty(reader[intReaderIndex].ToString()))
                    {
                        lsValidationInfo.Add("");
                    }
                    else
                    {
                        lsValidationInfo.Add(reader.GetString(intReaderIndex));
                    }
                    Console.WriteLine("reader index " + intReaderIndex + ": " + reader.GetString(intReaderIndex));
                }
            }
        }
    }

    // Use the items in lsValidationInfo to build the query to get the New Values
}

ExecuteReader()行上,尽管我得到了Npgsql.PostgresException,“操作符不存在:@字符变化'”。

让我感到困惑的是,我是在同一项目中成功完成相同功能的另一段代码之后进行建模的。它几乎是一个复制/粘贴,然后更改新功能的参数和对象名称。

c# postgresql npgsql executereader
1个回答
0
投票

所以我认为问题在于以下几行:

cmd.Parameters.Add(new NpgsqlParameter("@cateogry", strCategory));

尝试替换为以下内容:

cmd.Parameters.AddWithValue("category", strCategory);
© www.soinside.com 2019 - 2024. All rights reserved.