2011-03-31 88 views
0

我想通過應用程序與C#插入到PostgreSQL中的一行。按照Npgsql project homepage中顯示的步驟,我嘗試構建一個準備好的語句,以便在表中插入一行。我得到這個:問題與Npgsql準備

NpgsqlConnection conn = dbConn.getConnection(); 
conn.Open(); 
NpgsqlCommand query = new NpgsqlCommand("insert into table(c1, c2) values(:v1, :v2)", conn); 
query.Parameters.Add(new NpgsqlParameter("v1", NpgsqlDbType.Varchar)); 
query.Parameters.Add(new NpgsqlParameter("v2", NpgsqlDbType.Text)); 
query.Prepare(); 
query.Parameters[0].Value = "something"; 
query.Parameters[1].Value = "else"; 

而得到這個錯誤:

ERROR: 42601: syntax error in or near «:» 

什麼看法嗎?

在此先感謝

+0

烏胡,爲什麼你有:那裏呢?只是刪除它們。可能需要做@ v1,@ v2(無論是在查詢和參數中),不知道該連接器。 – stefan 2011-03-31 03:01:23

+0

就像示例所示。我會嘗試,謝謝 – Cheluis 2011-03-31 13:11:09

+1

你是否介意嘗試使用最新的Npgsql版本?請讓我知道,如果它不起作用。 – 2012-10-30 15:32:35

回答

0

這是從我工作的應用程序,所以我知道它的工作原理,如果它沒有讓我知道。

conn.Open(); 

NpgsqlCommand command = new NpgsqlCommand("DELETE from accounts where user_name = :value1", conn); 

// Now add the parameter to the parameter collection of the command specifying its type. 
command.Parameters.Add(new NpgsqlParameter("value1", NpgsqlDbType.Varchar)); 

//Now, add a value to it and later execute the command as usual.     
command.Parameters[0].Value = email; 
command.ExecuteNonQuery(); 

conn.Close(); 
+0

我剛剛意識到你想要一個準備。但這個例子可能對你仍然有用 – Arya 2012-04-25 06:40:24