1

我编写了使用 Microsoft Access 数据库插入和删除一些数据的代码,我可以插入数据,但是当我删除它时出现错误“data-type-mismatch-in-criteria-expression”我不知道为什么!有人帮我吗?

提前致谢 ;

  private void Savebt_Click(object sender, EventArgs e)
{
    //try
    //{
    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\me\Library Store\Library Store\Store.accdb");
     try
{
    conn.Open();

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = "INSERT INTO Libarary ( ISBN, [Name], Gategory, Author, Cost, [Date]) " +
          "VALUES ( @ISBN, @Name, @Gategory, @Author, @Cost, @Date) ";
    cmd.Parameters.AddWithValue("@ISBN", ISBNTB.Text);
    cmd.Parameters.AddWithValue("@Name", NameTB.Text);
    cmd.Parameters.AddWithValue("@Gategory", GategoryTB.Text);
    cmd.Parameters.AddWithValue("@Author", AuthorTB.Text);
    cmd.Parameters.AddWithValue("@Cost", int.Parse(CostTB.Text));
    cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text);



    cmd.ExecuteNonQuery();

        MessageBox.Show("Book Added!");
        conn.Close();


}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

}

private void sellbt_Click(object sender, EventArgs e)
{
     OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\me\Library Store\Library Store\Store.accdb");
     try
{
    conn.Open();

    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = " DELETE * FROM Libarary WHERE ISBN=@ISBN AND [Name]=@Name AND Gategory=@Gategory AND Author=@Author AND Cost=@Cost AND [Date]=@Date ";
    cmd.Parameters.AddWithValue("@ISBN", ISBNTB.Text);
    cmd.Parameters.AddWithValue("@Name", NameTB.Text);
    cmd.Parameters.AddWithValue("@Gategory", GategoryTB.Text);
    cmd.Parameters.AddWithValue("@Author", AuthorTB.Text);
    cmd.Parameters.AddWithValue("@Cost", CostTB.Text);
    cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text);

    cmd.ExecuteNonQuery();

        MessageBox.Show("Book removed to be sold!");
        conn.Close();


}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

错误与我尝试删除的记录 在此处输入图像描述

数据库记录 在此处输入图像描述

4

3 回答 3

0

尝试:

cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);

DateTimePicker.Text返回所选值的字符串表示形式,而不是值本身。

于 2013-08-04T13:32:06.737 回答
0

怎么样?

cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value.ToString("dd-MM-yyyy"));
于 2013-08-04T13:35:05.480 回答
0

您正面临此错误,因为您传递给查询的一个/多个参数与数据库中的类型不同。交叉检查它们。理想情况下应该像这样将参数传递给您的查询

cmd.Parameters.Add("@Date", OleDbType.Date); //note i have specified the db type
cmd.Parameters["@Date"].Value =dateTimePicker1.Value;

这将确保您具有与数据库中定义的类型相同的类型

于 2013-08-04T13:32:04.043 回答