c#里sql语句中where条件下的{}怎么使用?

i逐浪 2018-05-03 12:36:23
c#里sql语句中where条件下的{}怎么使用?
如下面学习案例中的{0}和{1}?
Form端和脚本处要怎么完善才能使用?

string P_Str_SqlStr = string.Format(//创建 SQL 查询字符串
@"SELECT 学生姓名,性别,年龄 FROM tb_Student WHERE 学生编号 IN
(SELECT 学生编号 FROM tb_Grade WHERE 总分>{0} AND 总分<{1})", Begin, end);

...全文
769 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2018-05-03
  • 打赏
  • 举报
回复
那是 string.Format 方法的占位符,表示其后参数的序号 你示例的代码中,运行结果(P_Str_SqlStr 的值)中 {0} 被 Begin 的值替换, {1} 被 end 的值替换
i逐浪 2018-05-03
  • 打赏
  • 举报
回复
谢谢楼上各位热心解答,结合各位意见调整语法,现已搞定 string SqlStr = string.Format(//创建 SQL 查询字符串 "select * from T_ORG_PositionHierarchy where FChildID ='{0}'",textBox14.Text);
i逐浪 2018-05-03
  • 打赏
  • 举报
回复
引用 9 楼 duanzi_peng 的回复:
还不正确,你要取的是textBox14的,不是textBox14 这文本框的名称。
建议,停下手中的工作,找本基础的书看看,把基础学的差不多了再开始编码。


谢谢您的提议。我本人是初学C#,主要想用C#封装一下日常反复使用SQL语句,方便平时查询。现在主要是下图的汇报关系无法处理。
exception92 2018-05-03
  • 打赏
  • 举报
回复
引用 7 楼 weixin_40894613 的回复:
引用 6 楼 duanzi_peng 的回复:
不正确,仔细理解#1楼的解释。
还不正确,你要取的是textBox14的,不是textBox14 这文本框的名称。 建议,停下手中的工作,找本基础的书看看,把基础学的差不多了再开始编码。
i逐浪 2018-05-03
  • 打赏
  • 举报
回复
引用 4 楼 qq_30465445 的回复:
你还可以这样写……在数据库把sql拼好 然后用$@ 后面就直接用{} 引用参数
以下这种写法可以吗? string SqlStr = $@" select 'N' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID={textBox14} union select 'N+1' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID={textBox14}) union select 'N+2' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={textBox14})) union select 'N+3' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={textBox14})))";
i逐浪 2018-05-03
  • 打赏
  • 举报
回复
引用 6 楼 duanzi_peng 的回复:
不正确,仔细理解#1楼的解释。
如果没有按#4的办法,应该要改成以下这样? string SqlStr = string.Format( @"select 'N' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID={0} union select 'N+1' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID={1}) union select 'N+2' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={2})) union select 'N+3' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={3})))",textBox14,textBox14,textBox14,textBox14);
exception92 2018-05-03
  • 打赏
  • 举报
回复
引用 5 楼 weixin_40894613 的回复:
[quote=引用 3 楼 duanzi_peng 的回复:] string.format 中的{}用来定义参数顺序及位置,和子查询没关系,处理过后的只是普通的字符串,只不过对于部分sql命令语句来说 看上去比较直观,编写语句时不容易出错而已,对于处理拼接字符串的场景比使用+或者+=符号更简便。
那以下的脚本是否正确?主要是textBox14文本框会被反复使用 string SqlStr = string.Format( @"select 'N' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID={0} union select 'N+1' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID={0}) union select 'N+2' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={0})) union select 'N+3' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={0})))",textBox14);[/quote] 不正确,仔细理解#1楼的解释。
i逐浪 2018-05-03
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
string.format 中的{}用来定义参数顺序及位置,和子查询没关系,处理过后的只是普通的字符串,只不过对于部分sql命令语句来说 看上去比较直观,编写语句时不容易出错而已,对于处理拼接字符串的场景比使用+或者+=符号更简便。
那以下的脚本是否正确?主要是textBox14文本框会被反复使用 string SqlStr = string.Format( @"select 'N' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID={0} union select 'N+1' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID={0}) union select 'N+2' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={0})) union select 'N+3' 上级,FLevel,FID,FChildID,FParentID from T_ORG_PositionHierarchy where FChildID=(select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID=( select FParentID from T_ORG_PositionHierarchy where FChildID={0})))",textBox14);
sprints_昊天 2018-05-03
  • 打赏
  • 举报
回复
你还可以这样写 string P_Str_SqlStr = $@" SELECT 学生姓名,性别,年龄 FROM tb_Student WHERE 学生编号 IN ( SELECT 学生编号 FROM tb_Grade WHERE 总分>{Begin} AND 总分<{end} )"; 在数据库把sql拼好 然后用$@ 后面就直接用{} 引用参数
exception92 2018-05-03
  • 打赏
  • 举报
回复
string.format 中的{}用来定义参数顺序及位置,和子查询没关系,处理过后的只是普通的字符串,只不过对于部分sql命令语句来说 看上去比较直观,编写语句时不容易出错而已,对于处理拼接字符串的场景比使用+或者+=符号更简便。
i逐浪 2018-05-03
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
那是 string.Format 方法的占位符,表示其后参数的序号 你示例的代码中,运行结果(P_Str_SqlStr 的值)中 {0} 被 Begin 的值替换, {1} 被 end 的值替换
CSDN上翻到的资料,有提到“String.format()用法”,只是没有用{}符号,这个是因为它在子查询语句里面才这样使用吗?SQL子查询直接用()就可以的了啊

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧