我有下面的C#代碼:C#String Concationation問題爲什麼不在這裏工作?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringTest
{
class Program
{
static void Main(string[] args)
{
String strSQLCode;
strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * "
+= " from view_dg_game_details gd (nolock) "
+= " where gd.gametypeid = {0} "
+= " and gd.numberofrounds = {1} "
+= " and gd.gamevalues = '{2}' ";
}
}
}
出於某種原因,我得到一個錯誤「賦值的左邊必須是一個變量,屬性或索引」。
我看不出錯誤在告訴我什麼。我已經評論了有問題的路線,但是錯誤只是上移了一條線。
我能得到的字符串concation使用這種方法的工作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringTest
{
class Program
{
static void Main(string[] args)
{
String strSQLCode;
strSQLCode = " select rank() over (order by percentagecorrect desc, totalmilliseconds asc) as rank, * ";
strSQLCode = strSQLCode + " from view_dg_game_details gd (nolock) ";
strSQLCode = strSQLCode + " where gd.gametypeid = {0} ";
strSQLCode = strSQLCode + " and gd.numberofrounds = {1} ";
strSQLCode = strSQLCode + " and gd.gamevalues = '{2}' ";
}
}
}
有人能向我解釋一下這個錯誤是什麼?
感謝
肯
您有SQL注入漏洞。 – SLaks 2012-02-14 18:53:13
字符串連接效率較低,在這裏最好使用'@'字符串。 – McKay 2012-02-14 18:56:37
@McKay:我想象編譯器在編譯時將它們結合起來。 – 2012-02-14 19:07:27