2015-08-08 47 views
2

我想在C#中使用Npgsql在PostgreSQL中的表中插入一個字符串數組。我已經編寫了下面的代碼,但得到了InvalidCastexception。使用Npgsql在postgresql中插入字符數組

eventcommand.Parameters.AddWithValue("@participants", NpgsqlDbType.Array).Value = participant.Text; 

其中參與者是一個文本框和eventcommand是NpgsqlCommand。

回答

2

你打電話給AddWithValue,但沒有提供價值 - 你提供類型。另外,你不提供數組 - 你只是提供一個字符串。我懷疑你只是想:

command.Parameters.Add("@participants", NpgsqlDbType.Array | NpgsqlDbType.Text).Value 
    = new[] { participant.Text }; 

或者你可能要participant.Text分割成字符串數組第一,或者類似的東西。

(我已經調整的類型按評論。)

+0

還要注意NpgsqlDbType.Array需要按位或運算與元素類型,所以NpgsqlDbType.Array |字符串數組的NpgsqlDbType.Text –

+0

@Shay:謝謝,修正。 –

+0

@JonSkeet&&Shay:非常感謝... –