C# 和 NpgsqlDataAdapter 返回单个字符串而不是数据表
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我明白了,它在 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.