C# 显示两个日期时间选择器之间的数据;“条件表达式中的数据类型不匹配”;

C# 显示两个日期时间选择器之间的数据;“条件表达式中的数据类型不匹配”;,c#,datetimepicker,datetime-format,C#,Datetimepicker,Datetime Format,我不知道这可能有什么问题 条件表达式中的数据类型不匹配 以下是我当前的代码: using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace NiceyBurgerJunction { public partial class frmSales : Form {

我不知道这可能有什么问题

条件表达式中的数据类型不匹配

以下是我当前的代码:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace NiceyBurgerJunction
{
    public partial class frmSales : Form
{

 OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\mariel\Desktop\NiceyBurgerJunction\NiceyBurgerJunctionDB.accdb");

int count;


    public frmSales()
    {
        InitializeComponent();
    }

    private void btnWeeklySales_Click(object sender, EventArgs e)
    {

        try
        {
            count = 0;
            DateTime from, to;

            from = Convert.ToDateTime(dateTimePicker1.Value);
            to = Convert.ToDateTime(dateTimePicker2.Value);

            dateTimePicker1.Format = DateTimePickerFormat.Short;
            dateTimePicker2.Format = DateTimePickerFormat.Short;

            con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT ProdName,SubTotal,OrderDate from EmployeeSale WHERE OrderDate BETWEEN '" + from.ToString("MM/dd/yyyy") + "' AND '" + to.ToString("MM/dd/yyyy") + "' ";
            cmd.ExecuteNonQuery();

            DataTable dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            da.Fill(dt);
            da.SelectCommand = cmd;
            count = Convert.ToInt32(dt.Rows.Count.ToString());
            dataGridView1.DataSource = dt;

            if (count == 0)
            {
                MessageBox.Show("No records found!");
            }

            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(" ERROR " + ex);
        }
    }
}


我的数据库设计的“OrderDate”格式正确,它的属性是Date/Time…

看起来传递给SQL语句的日期格式不正确。 我想应该是YYYY-MM-DD,而不是MM/DD/YYYY。 还考虑使用带有参数的语句。通过这种方式,您可以只传递您拥有的日期时间,而驱动程序将负责格式化工作。您只需使用
DbCommand.parameters.Add
API即可,然后将参数引用为
@from
@to

在此处阅读有关使用参数的内容:

此外,代码中也存在一些缺陷(例如使用
ExecuteNonQuery
执行一次命令,在无原因读取日期时间选择器的值后设置其格式…)


干杯

为什么不以文本形式共享错误消息?是什么触发了这个错误?我已经附上了这个错误的图片,先生。。在“在此输入图像描述”中。重点是:不要共享错误的图像。对不起,先生…哪一行抛出了错误?看看这个,应该会有帮助。