2010-03-22 223 views
1

我有一個postgresql數據庫和一個C#應用程序來訪問它。我遇到了一個奇怪的錯誤,我從NpgsqlDataAdapter.Fill命令返回的值返回到DataSet中。C#和NpgsqlDataAdapter返回單個字符串而不是數據表

我有這樣的代碼:

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,假,鮑勃·史密斯,7)」

而且數據是正確的,我會期待0,則5,然後是一個布爾值,然後是一些文本等。但我會(顯然)更喜歡它不會被返回爲一個大字符串。

任何人都知道爲什麼如果我做一個選擇*我得到一個數據表如預期,但如果我做了特定列的選擇,我得到一個數據表,我有一個條目是我要求的值的字符串?

回答

1

好吧,我想通了,它在SelectBuilder函數中。當select語句中列出多個列時,它將列中的()包含在內,顯然這會導致postgreSQL或Npgsql將其解釋爲希望以字符串形式返回列表。

相關問題