我必须在查询中传递一个字符串值...这是我的代码
string a = ConfigurationManager.AppSettings["var"];
SqlDataAdapter da = new SqlDataAdapter("select codesnippet from edk_custombrsnippet_vw where partnerid = 'a'", con);
DataTable dt = new DataTable();
da.Fill(dt);
Run Code Online (Sandbox Code Playgroud)
我得到了字符串中的值a ..但我不知道如何在该查询中传递该字符串.任何建议?
使用参数化查询,例如:
string a = ConfigurationManager.AppSettings["var"];
SqlDataAdapter da = new SqlDataAdapter(
"select codesnippet from edk_custombrsnippet_vw where partnerid = @PartnerID",
con);
da.SelectCommand.Parameters.AddWithValue("@PartnerId", a);
DataTable dt = new DataTable();
da.Fill(dt);
Run Code Online (Sandbox Code Playgroud)
你应该不是嵌入字符串值到SQL本身-打开你到SQL注入攻击.不可否认,在这种情况下,它不是用户提供的值,但最好不要直接包含它.
编辑:我改编自这个例子中这个MSDN网页,但似乎SqlDataAdapter并没有真正有一个Parameters属性,只要我能看到.因此,我已经适应它使用SqlDataAdapter.SelectCommand.Parameters它我希望的工作...但你可能需要稍微调整它.
| 归档时间: |
|
| 查看次数: |
338 次 |
| 最近记录: |