我应该将哪个字符用于dateTime(例如对于int我们使用%d)

0 c#

当我们想要将数据传递给数据库时,就像int我们使用(%d)一样

...string.format("select * from Table where code=%d",100)...

我应该使用什么,而不是%d当我们想要通过dateTime

Rav*_*dag 7

不要使用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)