使用参数化查询,您不必担心日期格式或SQL注入,并使用using以确保您的连接被处置.
using (var connection = new SqlConnection(yourConnectionString))
using (var command = connection.CreateCommand())
{
command.CommandText = "insert into YourTable(Col1, Col2) values(@val1, @val2)";
command.Parameters.AddWithValue("@val1", 123);
command.Parameters.AddWithValue("@val2", DateTime.Now);
connection.Open();
command.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4114 次 |
| 最近记录: |