0

我在数据库中有一个名为 date 的列,我的数据库 datecolumn 包含这样的 2013-02-22,但是在前端 iam 使用 2 个 ajaxdate 选择器并搜索记录我正在编写这样的查询,我想知道它是否是正确与否。

SELECT * from table where Date between '" + txtfromdate.Text +"' and '"+txttodate +"'";
4

1 回答 1

2

您的查询是正确的(假设您传递了格式正确的日期值),但除了一次编程练习外,其他任何事情都不能接受。

请使用参数化 SQL 查询以避免 SQL 注入。起点是SqlCommand.Parameters

这是您需要使用参数的近似代码:

SqlCommand command = new SqlCommand(
  "SELECT * FROM table WHERE Date BETWEEN @date1 AND @date2", connection);
command.Parameters.AddWithValue("@date1", DateTime.Parse(txtfromdate.Text));
command.Parameters.AddWithValue("@date1", DateTime.Parse(txttodate));
SqlDataReader reader = command.ExecuteReader();
于 2013-03-05T02:45:27.460 回答