【问题标题】:An unhandled exception of type 'System.Data.OleDb.OleDbException' Additional information: Data type mismatch in criteria expression'System.Data.OleDb.OleDbException' 类型的未处理异常附加信息:条件表达式中的数据类型不匹配
【发布时间】:2015-05-07 17:45:20
【问题描述】:

我正在尝试从组合框中的项目中获取价格。有什么想法吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Aplicatie_proiect
{
    public partial class Form2 : Form

    {
        private OleDbConnection connection = new OleDbConnection();
        public Form2()
        {
            InitializeComponent();
            connection.ConnectionString =    @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SleepyHitman-      V2\Documents\Inventory1.accdb;
    Persist Security Info=False;";
        }

        private void txt_order_Click(object sender, EventArgs e)
        {
            Double PT = new Double();
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "insert into Comenzi (Produs,Cantitate) values('"+txt_Produs.Text+"','"+txt_Cantitate.Text+"',)" ; //+ sa adaug useru care e logat din form 1
            command.ExecuteNonQuery();
            connection.Close();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select * from Inventory";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                txt_Produs.Items.Add(reader["Produs"].ToString());
            }
            connection.Close();

        }
        private void txt_Produs_SelectedIndexChanged(object sender, EventArgs e)
        {
            **connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "select * from Inventory where Pret ='" + txt_Produs.Text + "'";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                txt_Pret.Text = reader["Pret"].ToString();

            }
            connection.Close();**
        }
    }
 }

【问题讨论】:

  • 您应该更清楚地说明问题出在哪里,在哪里,以及您的预期。
  • 当我从组合框中选择一个项目时,我想获得价格(Pret),这样用户就可以看到该项目的价格是多少,然后选择数量。
  • 请编辑您的问题,不要在 cmets 中发布代码。那里看不懂。
  • 来自 txt_Produs_selectedIndexChanged 有我的问题也很抱歉我昨晚没睡,现在很难集中注意力 + 我在 20 分钟后要考试,我也试着重复一遍跨度>

标签: c# ms-access


【解决方案1】:

不清楚您在哪里得到错误。下次尝试更明确。

在上面的代码中,我看到您使用字符串连接来构建 sql 语句。由于 sql 注入和类型不匹配,这是一种不好的做法。

尝试改变

private void txt_order_Click(object sender, EventArgs e)
{
    Double PT = new Double();
    connection.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "insert into Comenzi (Produs,Cantitate) values('"+txt_Produs.Text+"','"+txt_Cantitate.Text+"',)" ; //+ sa adaug useru care e logat din form 1
    command.ExecuteNonQuery();
    connection.Close();
}

由此(我怀疑错误在于将 Cantitate -in a textBox- 转换为 int 或 double -in database-):

private void txt_order_Click(object sender, EventArgs e)
{
    Double PT = new Double();
    connection.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "insert into Comenzi (Produs,Cantitate) values(@product, @cantitate)" ; //+ sa adaug useru care e logat din form 1
    command.Parameters.AddWithValue("@product", txt_Produs.Text);
    command.Parameters.AddWithValue("@cantitate", Convert.ToInt32(txt_Cantitate.Text);
    command.ExecuteNonQuery();
    connection.Close();
}

【讨论】:

  • ty 指出我试图在那里改变一些东西并忘记撤消不幸的是这不是问题我仍然在这里得到错误'OleDbDataReader reader = command.ExecuteReader();'
  • 行内string query = "select * from Inventory where Pret ='" + txt_Produs.Text + "'";字段Pret是什么数据类型?
  • 它是字符串,因为我确实想让它保持简单,通常 Pret(price) 它是一个 int 但我后来解析了它,经过很多小时后我发现问题字符串 query = "select Pret from Inventory where Produs ='" + txt_Produs.Text + "'";
【解决方案2】:

我了解到您在这里比较的是数字字段。在这种情况下,您需要删除 sql 查询字符串中的单引号。

【讨论】:

    【解决方案3】:
    string query = "select Pret from Inventory where Produs ='" + txt_Produs.Text + "'";
    

    这是问题所在,请各位帮忙,希望下次我会更清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多