C#字符串的连接

来源:百度知道 编辑:UC知道 时间:2024/09/20 07:32:21
strSql_myselect = strSql_myselect + " where Name =' " + txtContent.Text + " ' "在这段代码中我勉强理解where Name =' " + txtContent.Text + " ' 这段的含义,但为什么在where Name 前面还要加双引号呢?它不是SQL语句的查询吗?怎么也把它看成了字符串呢?

不加双引号不就成了变量名了吗,你要向数据库服务其发送sql命令,那个命令是一个字符串,现在要在c#中构造一个字符串对吧

如果这条命令是 select * from talbe where name='aaa'

那么这条语句的字符串在c# 中就是 “select * from talbe where name='aaa'”
也就是
“select * from talbe ”+“where name='aaa'”
现在 前面的“select * from talbe ”被存放到了变量strSql_myselect 中
这个语句就变成了了

strSql_myselect +“where name='aaa'”

这就是双引号的来历

再进一步 aaa也是来自变量txtContent.Text
那么就变成了

strSql_myselect +“where name='”+txtContent.Text +“'”
这下明白了了吧

你的连接语句不全!
strSql_myselect应该是一个字符串变量,内容大概是 "select * from 表名"。
然后加上字符串"where name = ‘"
再加上name的值,也就是txtContent.text
因为name列在数据库里是varchar类型的,所以要加上单引号 “’”

整个语句是 "select * from 表名 where name ='值'"

一个sql语句就是一个 字符串。不要把sql语句和sql命令搞混了。
下面是一个sql命令
SqlCommand selectCommand=new SqlCommand(strSql_myselect,connetion);

上面的strSql_myselect就是查询字符串.<