gpt4 book ai didi

C# 潜在的日期格式问题 - System.Data.SqlClient.SqlException : 'Incorrect syntax near ' x'. '

转载 作者:行者123 更新时间:2023-11-30 23:00:48 25 4
gpt4 key购买 nike

该应用程序的目的是从 Sql 数据库中选择数据到 windows 窗体数据网格。我为日期时间选择器实现了自定义格式,但仍然收到此错误。这是应用程序的属性和以下代码

Date Time Picker Properties

        private void startTimePicker1_MouseDown(object sender, MouseEventArgs e)
{
startTimePicker1.CustomFormat = "yyyy-MM-dd hh:mm:ss";
startTimePicker1.Format = DateTimePickerFormat.Custom;
}

private void endTimePicker1_MouseDown(object sender, MouseEventArgs e)
{
endTimePicker1.CustomFormat = "yyyy-MM-dd hh:mm:ss";
endTimePicker1.Format = DateTimePickerFormat.Custom;
}

private void loadBtn_Click(object sender, EventArgs e)
{
startTimePicker1.CustomFormat = "yyyy-MM-dd hh:mm:ss";
startTimePicker1.Format = DateTimePickerFormat.Custom;

endTimePicker1.CustomFormat = "yyyy-MM-dd hh:mm:ss";
endTimePicker1.Format = DateTimePickerFormat.Custom;

using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DBName"].ConnectionString))
{
if(db.State==ConnectionState.Closed)
{
db.Open();
string query = "select z.ItemName, x.Quantity, x.CreateDate" +
" from Invoice_Itemized x inner join Inventory z on x.ItemNum = z.ItemNum inner join Departments d on z.Dept_ID = d.Dept_ID" +
$"where x.CreateDate BETWEEN '{ startTimePicker1 }' and '{ endTimePicker1 }' and d.SubType = 'TYPE'";

liquorBindingSource.DataSource = db.Query<Liquor>(query, commandType: CommandType.Text);
}
}

在运行时查询输出以下值

select z.ItemName, x.Quantity, x.CreateDate from Invoice_Itemized x inner join Inventory z on x.ItemNum = z.ItemNum inner join Departments d on z.Dept_ID = d.Dept_IDwhere x.CreateDate BETWEEN 'System.Windows.Forms.DateTimePicker, Value: 7/18/2018 2:44:00 AM' and 'System.Windows.Forms.DateTimePicker, Value: 7/18/2018 4:19:01 AM' and d.SubType = 'TYPE'

此查询在 SSMS 中成功执行,经过几个小时的调试,我遇到了一堵大墙。

最佳答案

string query = "select z.ItemName, x.Quantity, x.CreateDate" +
" from Invoice_Itemized x inner join Inventory z on x.ItemNum = z.ItemNum inner join Departments d on z.Dept_ID = d.Dept_ID" +
$"where x.CreateDate BETWEEN '{ startTimePicker1 }' and '{ endTimePicker1 }' and d.SubType = 'TYPE'";

liquorBindingSource.DataSource = db.Query<Liquor>(query, commandType: CommandType.Text);

这是令人难以置信的有害的,并且可能导致多种问题,包括格式/文化问题(它在某些机器上工作,但在其他机器上不起作用,具体取决于语言环境) - 和(更重要的)SQL注入(inject)风险。它还不允许查询计划缓存重用。

因为看起来您在这里使用的是“dapper”,所以 dapper 的设计使其易于参数化:

liquorBindingSource.DataSource = db.Query<Liquor>(@"
select z.ItemName, x.Quantity, x.CreateDate
from Invoice_Itemized x
inner join Inventory z on x.ItemNum = z.ItemNum
inner join Departments d on z.Dept_ID = d.Dept_ID
where x.CreateDate BETWEEN @start and @end and d.SubType = 'TYPE'",
new { start = startTimePicker1.Value, end = endTimePicker1.Value });

这将值作为类型化参数传递(假设 .ValueDateTime),避免了所有本地问题消除了风险SQL 注入(inject)。如果 .Value 不是 DateTime,而只是 string,那么我会使用:

new { start = DateTime.Parse(startTimePicker1.Value),
end = DateTime.Parse(endTimePicker1.Value) }

作为第二个参数。

关于C# 潜在的日期格式问题 - System.Data.SqlClient.SqlException : 'Incorrect syntax near ' x'. ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51398303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com