0 c#
当我们想要将数据传递给数据库时,就像int我们使用(%d)一样
...string.format("select * from Table where code=%d",100)...
我应该使用什么,而不是%d当我们想要通过dateTime?
不要使用string.format()进行db参数替换,最后是SQL注入.SqlCommand具有Parameters属性,可以将参数添加到集合中
使用参数化查询,如下所示:
SqlCommand cmd = new SqlCommand()
cmd.Parameters.AddWithValue("@yourParam",value);
Run Code Online (Sandbox Code Playgroud)
如果对代码使用"使用"块,则效率更高.
像这样
using(SqlConnection con = new SqlConnection("ConnString"))
using(SqlCommand cmd = new SqlCommand(con))
{
// do some stuffs. like add parameters.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |