1

我正在尝试通过使用 C# 的应用程序将一行插入 PostgreSQL。按照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 «:»

有什么意见吗?

提前致谢

4

1 回答 1

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();
于 2012-04-25T06:33:09.920 回答