C# 字符串报错 常量中有换行符

代码如下,我写了个函数,传递一个h字符串变量。字符串sql中我加了h的变量在中间,也加了@,但是总报错。提示很多错误:包括:常量中有换行符、字符文本中的字符太多、空字符、... 代码如下,我写了个函数,传递一个h字符串变量。字符串sql中我加了h的变量在中间,也加了@,但是总报错。提示很多错误:包括:常量中有换行符、字符文本中的字符太多、空字符、应输入;、应输入]等很多错误,但是我本身sql语句应该是正确的啊。去掉了h的变量变成纯string 就好。
public void getData(string h)
{ int xuhao = int.Parse(data.getdata("select max(NO) from gk_生产消耗 where 生产主机='"+h+"'", connstr));
int gkxuhao = int.Parse(data.getdata("select max(xuhao) from ",gkconnstr));
//下面是报错的字符串
string sql=@"SELECT xuhao as NO, '"+h+"' AS 生产主机,datetime1 AS 生产时间,xufang AS 生产方量, 0 AS 当前盘次,
Peigl1 AS [1理论值], GL1 AS [1实际值], Peigl2 AS [2理论值], GL2 AS [2实际值],
PeiGL3 AS [3理论值], GL3 AS [3实际值],PeiGL4 AS [4理论值], GL4 AS [4实际值],
Peisn1 AS [5理论值], sn1 AS [5实际值],Peisn2 AS [6理论值], sn2 AS [6实际值],
Peisn3 AS [7理论值], sn3 AS [7实际值], Peishui AS [8理论值],shui AS [8实际值],
Peiye1 AS [9理论值], ye1 AS [9实际值],Peiye2 AS [10理论值],ye2 AS [10实际值],
Peidaye1 AS [11理论值], daye1 AS [11实际值],Peimei1 AS [12理论值], fmei1 AS [12实际值],
Peimei2 AS [13理论值], fmei2 AS [13实际值],Peimei3 AS [14理论值], fmei3 AS [14实际值],
Peifen1 AS [15理论值], fen1 AS [15实际值],Peifen2 AS [16理论值], fen2 AS [16实际值],
peibi AS 任务单号, peibi AS 配比单号, sgdw AS 单位名称, gcmc AS 工程名称, Address AS 工程地址, buwei AS 施工部位, '' AS 浇注方式, '' AS 出单时间,shusongche AS 车辆编号, siji AS 司机, biaohao AS 产品标号, caozuoyuan AS 生产人员,'' AS 运输单号, '' AS 生产线,0 as 本车方量,case when (zhonglei='砂浆') then 1 else 0 end as 是否砂浆 from [GK_海诺工控1耗料]
where xuhao >"+xuhao+" and host="+h+"";
..........
}
展开
 我来答
阳光的雷咩咩
2014-12-27 · TA获得超过1.4万个赞
知道大有可为答主
回答量:2.3万
采纳率:66%
帮助的人:7870万
展开全部

错误太多,没法给你改,不过你可以按照下面的格式来改:

string sql=string.Format(@"固定字符串1{0}固定字符串2'{1}'",变量0,变量1);
更多追问追答
追问
谢谢哈,我把字符串给简化了一下。string s=@"select xuhao as NO,'"+h+"' as 生产主机
from table ";
它也报错,是不是有变量的话不能随便换行啊。我也加@了
追答
我教你用format你没用。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式