[英]c# Sqlcommand error
foreach (string str in TestWords)
{
//spam
SqlCommand cmd6 = new SqlCommand("select count from keys,files,folders where keys.fileid=files.id and keys.kname='" + str + "' and files.spam=1 and folders.id<>" + FolIter + " and files.folderid<>" + FolIter + " and files.id='" + s[0].ToString + "'", cn);
int i6 = Convert.ToInt16(cmd6.ExecuteScalar());
double temp = Convert.ToDouble((i6 + 1) / (i7 + i8));
//non spam
**error**
SqlCommand cmd9 = new SqlCommand("select count from keys,files,folders where keys.fileid=files.id and keys.kname='"
+ str
+ "' and files.spam=0 and folders.id<>"
+ FolIter
+ " and files.folderid<>"
+ FolIter
+ " and files.id='"
+ s[0].ToString
+ "'", cn);
int i9 = Convert.ToInt16(cmd9.ExecuteScalar());
temp2 = Convert.ToDouble((i9 + 1) / (i7 + i8));
Sdoc = Convert.ToDouble(Sdoc * temp);
NsDoc = Convert.ToDouble(NsDoc * temp2);
}
The error iam getting is: Operator '+' cannot be applied to operands of type 'string' and 'method group' iam得到的错误是: 运算符“ +”不能应用于类型为“字符串”和“方法组”的操作数
您必须调用该方法:
s[0].ToString()
As Nix, Femaref and Azhar mentioned, .ToString() is the typo that triggers the error message. 正如Nix,Femaref和Azhar所提到的,.ToString()是触发错误消息的错字。
May I suggest to use parameters instead of string concatenation ? 我可以建议使用参数而不是字符串连接吗? This way:
这条路:
SqlCommand cmd9 = new SqlCommand("select count from keys,files,folders where keys.fileid=files.id and keys.kname=@name and and files.spam=0 and folders.id<>@FolIter and files.folderid<>@FolIter and files.id=@s0", cn);
cmd9.Parameters.Add(new SqlParameter("@name", str));
cmd9.Parameters.Add(new SqlParameter("@FolIter", FolIter));
cmd9.Parameters.Add(new SqlParameter("s0", s0));
By this way, ADO.NET will deal with your variable as is, you wont have to convert them to string to use concatenation, and you wont be exposed to a SQL injection risk. 通过这种方式,ADO.NET将按原样处理您的变量,您不必将它们转换为字符串即可使用串联,也不会面临SQL注入风险。
You are using method ToString()
as Property 您正在使用方法
ToString()
作为属性
change s[0].ToString
-> s[0].ToString()
更改
s[0].ToString
> s[0].ToString()
Remember C# does not allow it. 请记住,C#不允许这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.