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

时间:2010-03-22 20:30:58

标签: c# postgresql datatable npgsql

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

我有这段代码:

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,假,Bob Smith,7)”

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

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

1 个答案:

答案 0 :(得分:1)

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