我在 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/