![](/img/trans.png)
[英]System.Data.OleDb.OleDbException: Data type mismatch in criteria expression
[英]An unhandled exception of type 'System.Data.OleDb.OleDbException' Additional information: Data type mismatch in criteria expression
我正在嘗試從組合框中的項目中獲取價格。有什么想法嗎?
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();**
}
}
}
目前尚不清楚您在哪里得到錯誤。 下次嘗試更加明確。
在abobe代碼中,我看到您使用字符串連接來構建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-在textBox-中轉換為int或double -in數據庫-中):
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();
}
我了解到您正在此處比較數字字段。 在這種情況下,您需要刪除sql查詢字符串中的單引號。
string query = "select Pret from Inventory where Produs ='" + txt_Produs.Text + "'";
這就是問題所在,請幫忙的人希望下次我會更加清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.