[英]Data type mismatch in criteria expression exception in ms acess database
I'm creating a simple program about now the idea is to insert the data "Person information Name, Age ETC. " inside the database through my c# program and I when I click the button I get this error 我现在正在创建一个简单的程序,这个想法是通过c#程序在数据库中插入数据“ Person information Name,Age ETC.”,当我单击按钮时,出现此错误
System.Data.OleDb.OleDbException (0x80040E07): Data type mismatch in criteria expression.
System.Data.OleDb.OleDbException(0x80040E07):条件表达式中的数据类型不匹配。 at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at School_System.newRegisteration.button1_Click(Object sender, EventArgs e) in C:\\Users\\OmarS_000\\documents\\visual studio 2015\\Projects\\School System\\School System\\newRegisteration.cs:line 35
在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object&executeResult)在System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(System.Data.OleDb.OleDbCommand.ExecuteCommandText(Data&Execute。) System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior行为,String方法)的System.Data.OleDb.OleDbCommand.ExecuteNonQuery()的OleDb.OleDbCommand.ExecuteCommand(CommandBehavior行为,Object&executeResult),School_System.newRegisteration.button1_Click ,EventArgs e),位于C:\\ Users \\ OmarS_000 \\ documents \\ visual studio 2015 \\ Projects \\学校系统\\学校系统\\ newRegisteration.cs:第35行
Here is my code 这是我的代码
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;
using System.ComponentModel;
public partial class newRegisteration : Form
{
private OleDbConnection connection = new OleDbConnection();
public newRegisteration()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\OmarS_000\Documents\Visual Studio 2015\Projects\School System\School System\School.accdb;
Persist Security Info=False;";
}
private void button1_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2 + "', '" + ageTextBox2 + "', '" + gradeTextBox2 + "', '" + classTextBox2 + "') ";
command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
}
line 35 which the error mentioned it contains 错误提到的第35行包含
command.ExecuteNonQuery();
i get this error when i click on the button, but the codes in the visual studio debugs perfectly with 0 errors. 当我单击按钮时出现此错误,但是Visual Studio中的代码完全调试为0错误。 so what spot did i miss right here ?
所以我在这里错过了什么地方?
您正在使用某些文本框中的值,因此需要使用其Text
属性来获取值
command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2.Text + "', '" + ageTextBox2.Text + "', '" + gradeTextBox2.Text + "', '" + classTextBox2.Text + "') ";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.