Datetyoe在标准表达式中不匹配 [英] Datetyoe mismatch in criteria expression

查看:79
本文介绍了Datetyoe在标准表达式中不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys,我收到的错误

条件表达式中的Datetyoe不匹配

一切都很完美但是当我添加日期列时所以我得到这个错误请大家解决吧



我的尝试:



 con_string.Open(); 
OleDbCommand command = new OleDbCommand(){Connection = con_string};
for(int i = 0; i< dataGridView1.Rows.Count; i ++)
{
command.CommandText = @insert into [Total]([column3],[column2] ,[Flavors],[备注],[Column1],[Extra Topping],[Topping Price],[Date])
VALUES(@ Column3,@ Column2,@ Flavours,@ Comments,@ Column1,@ ExtraTopping ,@ ToppingPrice,@ Date);
command.Parameters.Clear();
command.Parameters.AddWithValue(@ Column3,dataGridView1.Rows [i] .Cells [0] .Value);
command.Parameters.AddWithValue(@ Column2,dataGridView1.Rows [i] .Cells [0] .Value);
command.Parameters.AddWithValue(@ Flavors,dataGridView1.Rows [i] .Cells [0] .Value);
command.Parameters.AddWithValue(@ Remarks,dataGridView1.Rows [i] .Cells [0] .Value);
command.Parameters.AddWithValue(@ Column1,dataGridView1.Rows [i] .Cells [0] .Value);
command.Parameters.AddWithValue(@ ExtraTopping,dataGridView1.Rows [i] .Cells [0] .Value);
command.Parameters.AddWithValue(@ ToppingPrice,dataGridView1.Rows [i] .Cells [0] .Value);
command.Parameters.AddWithValue(@ Date,dataGridView1.Rows [i] .Cells [0] .Value.ToString());

command.ExecuteNonQuery();
}
con_string.Close()

解决方案

不要将日期转换为字符串以传递它到SQL:如果它已经是DateTime值,则将其传递给它。如果没有,请使用TryParse将其转换为DateTime,然后传递给它。



字符串日期的问题在于它们对于所有系统都不相同: SQL Server实例的语言环境可能与运行代码的语言环境不同,看起来没什么问题23/12/10对SQL来说看起来很糟糕,因为它期待MM / dd / yyyy并且你传递了yy / MM / dd。

我也非常怀疑同一行和列是否包含您尝试插入的所有列的正确值 - 您是否注意到每个参数都使用相同的单元格?

Hello Guys, am getting an error that is

Datetyoe mismatch in criteria expression

everything is perfect fine but when i add date columns so i get this error please guys solve it please

What I have tried:

con_string.Open();
                    OleDbCommand command = new OleDbCommand() { Connection = con_string };
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        command.CommandText = @"insert into [Total]([column3],[column2],[Flavours],[Remarks],[Column1],[Extra Topping],[Topping Price],[Date])
                        VALUES (@Column3, @Column2, @Flavours, @Remarks, @Column1, @ExtraTopping, @ToppingPrice,@Date)";
                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@Column3", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Column2", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Flavours", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Remarks", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Column1", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@ExtraTopping", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@ToppingPrice", dataGridView1.Rows[i].Cells[0].Value);
                        command.Parameters.AddWithValue("@Date", dataGridView1.Rows[i].Cells[0].Value.ToString());

                        command.ExecuteNonQuery();
                    }
                    con_string.Close()

解决方案

Don't convert the date to a string to pass it to SQL: if it's a DateTime value already, pass it as such. If not, use TryParse to convert it to a DateTime, and then pass that.

The trouble with string dates is that they aren't the same for all systems: the locale for an SQL Server instance can be different from that your code runs on, and what looks ok to you "23/12/10" looks bad to SQL because it's expecting MM/dd/yyyy and you passed it yy/MM/dd.
I also seriously doubt that the same row and column contains the right value for all of the columns you are trying to INSERT it into - you had noticed that you use the same cell for every parameter?


这篇关于Datetyoe在标准表达式中不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆