简体   繁体   English

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

[英]Npgsql.PostgresException error on ExecuteReader when attempting to query PostgreSQL database in C# Winform

I'm trying to get a list of strings from a database query and put those in a ComboBox.我正在尝试从数据库查询中获取字符串列表并将它们放入 ComboBox 中。

The code that I'm attempting to do that with starts with the following on my from in a DataGridVew Cell Click event:我尝试执行此操作的代码以我在DataGridVew Cell 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);
}

I need to use the above query to find out what table and fields I need to for the query to populate lsNewValuesResult .我需要使用上面的查询来找出查询填充lsNewValuesResult所需的表和字段。

In GetNewValuesList I have this code: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
}

On the ExecuteReader() line, though I'm getting an Npgsql.PostgresException, "operator does not exist: @ character varying'".ExecuteReader()行上,尽管我收到 Npgsql.PostgresException,但“运算符不存在:@ character varying”。

What's puzzling me is that I modeled this after another block of code in the same project that does the same thing successfully.令我困惑的是,我在同一个项目中成功完成同样事情的另一代码块之后对此进行了建模。 It was pretty much a copy/paste and then change the parameter and object names for the new function.这几乎是一个复制/粘贴,然后更改新 function 的参数和 object 名称。

So i think problem is with following line:所以我认为问题出在以下行:

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

Try replacing with following:尝试替换为以下内容:

cmd.Parameters.AddWithValue("category", strCategory);

Just another way to write it, with this you can also provide other attributes like Parameter direction like OUT and INOUT as well-只是另一种编写方式,您还可以提供其他属性,例如OUTINOUT等参数方向 -

cmd.Parameters.Add(new NpgsqlParameter("@cateogry", NpgsqlTypes.NpgsqlDbType.Varchar){ Value = strCategory,Direction= ParameterDirection.Input });

Thanks!谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#中的Postgresql Npgsql.PostgresException - Postgresql Npgsql.PostgresException in c# PostgreSQL:负载测试抛出错误 - Npgsql.PostgresException:“53300:抱歉,已经有太多客户端” - PostgreSQL: Load testing throws error - Npgsql.PostgresException: '53300: sorry, too many clients already' C#与SQLEditor — Npgsql.PostgresException 42703:“用户名”列不存在 - C# vs SQLEditor — Npgsql.PostgresException 42703: column “username” does not exist Npgsql.PostgresException 过程未找到异常 - Npgsql.PostgresException procedure not found exception Npgsql.PostgresException:关系“表名”不存在&#39; - Npgsql.PostgresException: relation "tablename" does not exist' 连接到 Redshift 集群时出错 - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host - Error connecting to Redshift Cluster - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host 在C#中以数据库不可知的方式查询PostgreSQL db(即不使用Npgsql) - Query PostgreSQL db in C# in database agnostic way (ie without Npgsql) Npgsql.PostgresException:&#39;55000:在此会话中尚未定义序列“ student_folio_id_seq”的当前时间” - Npgsql.PostgresException: '55000: currval of sequence “student_folio_id_seq” is not yet defined in this session' Npgsql.PostgresException (0x80004005): 42883: 运算符不存在: jsonb #&gt; text - Npgsql.PostgresException (0x80004005): 42883: operator does not exist: jsonb #> text Npgsql.PostgresException:&#39;42P01:关系“表”不存在” - Npgsql.PostgresException: '42P01: relation “table” does not exist'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM