c# - 将带有值的变量传递给 postgresql 数据库

标签 c# mysql database variables

我在 C# Visual studio 2010 中创建了此方法。ofd.Filename 实际上是硬盘驱动器上文件位置的路径。该方法连接MYSQL数据库,效果非常好。然而在 Postgresql 中,带有@符号的变量 imagepath 根本无法被识别。事实上,错误消息是“找不到未知列 imagepath”,但 imagepath 是一个值而不是数据库中的列。将 @imagepath 用单引号括起来不会传递 ofd.FileName 的值,而是将文字传递到数据库中。有谁知道在postgresql中将变量值传递到数据库的方法吗?

string imagepath = ofd.FileName;
string connect = ("Server=localhost;Port=1006;Database=collegestudents;Uid=roberto;Pwd=****;");
MySqlConnection con = new MySqlConnection(connect);
string Query = ("UPDATE student SET studentpix = @imagepath WHERE pk_studentID = '?' OR pk_studentID = pk_studentID "); 
MySqlCommand cmd = new MySqlCommand(Query, con);
cmd.Parameters.AddWithValue("@imagepath",ofd.FileName);   

最佳答案

对于 postgresql 参数前缀为 : 而不是 @,请尝试此操作

string Query = ("UPDATE student SET studentpix = :imagepath WHERE pk_studentID = '?' OR pk_studentID = pk_studentID "); 
MySqlCommand cmd = new MySqlCommand(Query, con);
cmd.Parameters.AddWithValue("imagepath",ofd.FileName);   

关于c# - 将带有值的变量传递给 postgresql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16653155/

相关文章:

sql - 仅在 VoltDB 中更新必要的字段

MySQL - 为什么 count() 会改变外连接的行为?

php - 如何将文本文件直接或通过 PHP 导入 MySQL

c# - 公共(public)交通临时队列

c# - LINQ to Entities 无法识别方法 'System.String[] ToArray[String]

mysql - 有没有办法在 SELECT 语句中将所有列中的零长度字符串字段(一次)替换为 NULL?

java - ResultSetExtractor 中 getInt 的性能问题

PHP : inserting row id to another table

c# - 使用 Linq 和 SQL 时如何防止将新记录插入表中?

c# - 从 List<Dictionary<DateTime, Points[]>> 转换为 Dictionary<DateTime, Points[]>