[英]Ignore single quote in C#
在我的程序中,我有這句話……
ct = String.Format(@"select [Movie Selector] from WSTMDS3
natural prediction join
(select
'{0}'as[Age],
'{1}'as[Education Level],
'{2}'as[Gender],
'{3}'as[Home Ownership],
'{4}'as[Internet Connection],
'{5}'as[Marital Status]
)as MovieSelector",
TextBox1.Text,
DropDownList1.SelectedValue,
DropDownList2.SelectedValue,
DropDownList3.SelectedValue,
DropDownList4.SelectedValue,
DropDownList5.SelectedValue)
;
但是在Dropdown list1“ Education Level”中,我有一些價值,例如碩士學位,我如何在本陳述中使用單引號。
謝謝
您不應使用string.Format
來構建查詢。 您應該使用參數化查詢。
adomdCommand.CommandText = "SELECT ... @P1 ...";
adomdCommand.Parameters.Add(new AdomdParameter("P1", TextBox1.Text));
// etc..
有關
使用SqlCommand和SqlParameter。 例
SqlCommand sqlCom=new SqlCommand();
sqlCom.CommandText=@"select [Movie Selector] from WSTMDS3
natural prediction join
(select
@P0 as[Age],
@P1 as[Education Level],
@P2 as[Gender],
@P3 as[Home Ownership],
@P4 as[Internet Connection],
@P5 as[Marital Status]
";
sqlCom.Parameters.AddWithValue("@P0",TextBox1.Text);
除了使用參數化查詢外,T-SQL語法中單引號的轉義方法是將它們加倍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.