简体   繁体   English

C#生成查询以将DateTime与SQL Nvarchar date列进行比较

[英]C# Generate Query to Compare DateTime with SQL Nvarchar date Column

I tried to get result depending on two dates which the user checked. 我试图根据用户检查的两个日期来获取结果。 I have two datetimepicker controls. 我有两个datetimepicker控件。 I want the user to chooses the "from" date and "to" date, then the query get specific result. 我希望用户选择“开始”日期和“结束”日期,然后查询获得特定结果。

leaving_time column type is nvarchar leaves_time列的类型为nvarchar

This is my query: 这是我的查询:

SELECT name, mil_no, rotba, arrival_time, leaving_time, day, year
FROM dbo.Hodor_data 
WHERE leaving_time BETWEEN '"+dateTimePicker1.Checked.ToString()+ "' AND  '" + dateTimePicker2.Checked.ToString() + '"

Where is the mistake? 错误在哪里?

You should write parameterized queries and not using string concatenation for passing the parameters, in order to create a sql command. 您应该编写参数化查询,而不要使用字符串连接来传递参数,以便创建sql命令。 Using string concatenation makes you code vulnerable to sql injections. 使用字符串连接使您的代码容易受到sql注入的攻击。

var cmdText = @"SELECT ...
                FROM dbo.Hodor_data
                WHERE leaving_time BETWEEN @StartDate AND @EndDate";

var sqlCommand = new SqlCommand(cmdText, connection);
sqlCommand.Parameters.AddWithValue("@StartDate", dateTimePicker1.Value);
sqlCommand.Parameters.AddWithValue("@EndDate", dateTimePicker2.Value);

where connection is your sql connection object. 其中connection是您的sql连接对象。

尝试在使用dateTimePicker1.TextdateTimePicker1_ValueChanged事件中使用dateTimePicker2.Checked已检查返回truefalse而不是date的值

Checked is a boolean property, and it is not the date. Checked是一个布尔型属性,它不是日期。 You need to use the Value Property. 您需要使用值属性。 It is better to add parameters and explicitly specify the type so that the date format conflict is solved. 最好添加参数并显式指定类型,以便解决日期格式冲突。

Edit: If column type in SQL server is NVARCHAR and of format MM/dd/yyyy , you need to use ONVERT(DATETIME, leaving_time, 101) : 编辑:如果SQL Server中的列类型为NVARCHAR且格式为MM/dd/yyyy ,则需要使用ONVERT(DATETIME, leaving_time, 101)

conn.Open();
SqlDataAdapter dataAdapter = 
    new SqlDataAdapter("SELECT name, mil_no, rotba, arrival_time, leaving_time, day, year "
                     + "FROM dbo.Hodor_data where CONVERT(DATETIME, leaving_time, 101) "
                     + "BETWEEN @p1 AND @p2", conn);

SqlParameter fromDate = new SqlParameter("@p1", SqlDbType.DateTime2);
fromDate.Value = dateTimePicker1.Value;
SqlParameter toDate = new SqlParameter("@p2", SqlDbType.DateTime2);
toDate.Value = dateTimePicker2.Value;

dataAdapter.SelectCommand.Parameters.Add(fromDate);
dataAdapter.SelectCommand.Parameters.Add(toDate);

DataTable dt = new DataTable();
dataAdapter.Fill(dt);
dataGridView1.DataSource = dt; 
conn.Close()

You should really consider changing the type of column leaving_time to be a DateTime column. 您应该真正考虑将leaving_time列的类型更改为DateTime列。 This will make your life easier in querying. 这将使您的查询生活更加轻松。 I can't really see any advantage of storing these values as text. 我真的看不到将这些值存储为文本的任何优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM