c# – 类似于使用参数的npgsql语句
发布时间:2020-12-15 04:31:59 所属栏目:百科 来源:网络整理
导读:我有一个 postgresql数据库,我想查询表“位置”,以检索与用户输入的名称匹配的所有位置的名称.列名是“LocationName”.我正在使用带有C#的ASP.net. NpgsqlConnection con = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ToSt
我有一个
postgresql数据库,我想查询表“位置”,以检索与用户输入的名称匹配的所有位置的名称.列名是“LocationName”.我正在使用带有C#的ASP.net.
NpgsqlConnection con = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ToString()); NpgsqlCommand cmd = new NpgsqlCommand("Select * from "Locations" where "LocationName" LIKE "%@loc_name%"",con); cmd.Parameters.AddWithValue("@loc_name",Location_Name); NpgsqlDataReader reader = cmd.ExecuteReader(); 我得到这个例外: Npgsql.NpgsqlException: ERROR: 42703: column "%((E'My place'))%" does not exist 我尝试不使用%运行查询,但它不起作用. string query = "Select "LocationName" from "Locations" where "LocationName" LIKE '%'+ :loc_name +'%'"; 使用上面的行,我得到这个例外: Npgsql.NpgsqlException: ERROR: 42725: operator is not unique: unknown + unknown 解决方法
你应该使用
NpgsqlCommand cmd = new NpgsqlCommand("Select * from "Locations" where "LocationName" LIKE @loc_name",con); cmd.Parameters.AddWithValue("@loc_name","%" + Location_Name + "%"); 你插入了太多的引号:Postgre将双引号之间的字符串解释为字段/表名.让参数执行转义字符串作业 P.S.:要在Postgre中连接字符串,你应该使用||运算符,请参阅here.所以您的上一个查询应该是 string query = "Select "LocationName" from "Locations" where "LocationName" LIKE '%' || :loc_name || '%'"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- xcode – 无法执行操作,因为“PROJECTNAME”有一
- JSONArray jsonArray解析
- xcode – Swift playground打印括号
- oracle – SQLDeveloper – 在另一个模式中查看和
- geoserver、openlayers、PostgreSQL 开发环境配置
- c# – 拉伸图像以填充可用的宽度/高度(单独)
- AngularJs expression详解及简单示例
- C#/ Razor – 从RazorEngine模板访问本地化的字符
- rvm install ruby??-1.8.7-p334给出错误“你传递
- ruby-on-rails – 如何编写一个按月返回计数的ra
热点阅读