C# 和 NpgsqlDataAdapter 返回单个字符串而不是数据表

发布于 2024-08-25 17:40:25 字数 1597 浏览 5 评论 0原文

我有一个 postgresql 数据库和一个 C# 应用程序来访问它。我从 NpgsqlDataAdapter.Fill 命令返回到数据集的值出现了奇怪的错误。

我有这个代码:

NpgsqlCommand n = new NpgsqlCommand();
n.Connection = connector; // a class member NpgsqlConnection

DataSet ds = new DataSet();
DataTable dt = new DataTable();

// DBTablesRef are just constants declared for
// the db table names and columns

ArrayList cols = new ArrayList();
cols.Add(DBTablesRef.all); //all is just *

ArrayList idCol = new ArrayList();
idCol.Add(DBTablesRef.revIssID);

ArrayList idVal = new ArrayList();
idVal.Add(idNum); // a function parameter

// Select builder and Where builder are just small
// functions that return an sql statement based
// on the parameters.  n is passed to the where
// builder because the builder uses named  
// parameters and sets them in the NpgsqlCommand
// passed in
String select = SelectBuilder(DBTablesRef.revTableName, cols) +
WhereBuilder(n,idCol, idVal);

n.CommandText = select;

try
{
    NpgsqlDataAdapter da = new NpgsqlDataAdapter(n);

    ds.Reset();

    // filling DataSet with result from  NpgsqlDataAdapter
    da.Fill(ds);

    // C# DataSet takes multiple tables, but only the first is used here
    dt = ds.Tables[0];
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

所以我的问题是这样的:上面的代码工作完美,就像我想要的那样。但是,如果我尝试命名要从查询返回的各个列,而不是对所有 (*) 进行选择,我会得到我要求的信息,但不会在数据表中分成单独的条目,而是得到一个字符串在数据表的第一个索引中,看起来像:

“(0,5,false,Bob Smith,7)”

并且数据是正确的,我期望0,然后是5,然后是布尔值,然后是一些文本等但我(显然)不希望它只是作为一个大字符串返回。

任何人都知道为什么如果我对 * 进行选择,我会按预期得到一个数据表,但如果我对特定列进行选择,我会得到一个数据表,其中一个条目是我要求的值的字符串?

I have a postgresql db and a C# application to access it. I'm having a strange error with values I return from a NpgsqlDataAdapter.Fill command into a DataSet.

I've got this code:

NpgsqlCommand n = new NpgsqlCommand();
n.Connection = connector; // a class member NpgsqlConnection

DataSet ds = new DataSet();
DataTable dt = new DataTable();

// DBTablesRef are just constants declared for
// the db table names and columns

ArrayList cols = new ArrayList();
cols.Add(DBTablesRef.all); //all is just *

ArrayList idCol = new ArrayList();
idCol.Add(DBTablesRef.revIssID);

ArrayList idVal = new ArrayList();
idVal.Add(idNum); // a function parameter

// Select builder and Where builder are just small
// functions that return an sql statement based
// on the parameters.  n is passed to the where
// builder because the builder uses named  
// parameters and sets them in the NpgsqlCommand
// passed in
String select = SelectBuilder(DBTablesRef.revTableName, cols) +
WhereBuilder(n,idCol, idVal);

n.CommandText = select;

try
{
    NpgsqlDataAdapter da = new NpgsqlDataAdapter(n);

    ds.Reset();

    // filling DataSet with result from  NpgsqlDataAdapter
    da.Fill(ds);

    // C# DataSet takes multiple tables, but only the first is used here
    dt = ds.Tables[0];
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}

So my problem is this: the above code works perfectly, just like I want it to. However, if instead of doing a select on all (*) if I try to name individual columns to return from the query I get the information I asked for, but rather than being split up into seperate entries in the data table I get a string in the first index of the data table that looked something like:

"(0,5,false,Bob Smith,7)"

And the data is correct, I would be expecting 0, then 5, then a boolean, then some text etc. But I would (obviously) prefer it to not be returned as just one big string.

Anyone know why if I do a select on * I get a datatable as expected, but if I do a select on specific columns I get a data table with one entry that is the string of the values I'm asking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-09-01 17:40:25

好吧,我明白了,它在 SelectBuilder 函数中。当 select 语句中列出多个列时,它会将这些列包装在 () 中,显然这会导致 postgreSQL 或 Npgsql 将其解释为希望以字符串形式返回列表。

Ok, I figured it out, it was in the SelectBuilder function. When more than one column was listed in the select statement it was wrapping the columns in ()'s, and apparently this causes either postgreSQL or Npgsql to interpret that as a desire to return a list in string form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文