ms access 数据库中标准表达式异常中的数据类型不匹配
Data type mismatch in criteria expression exception in ms acess database
我正在创建一个简单的程序,现在的想法是通过我的 C# 程序将数据 "Person information Name, Age ETC. " 插入数据库,当我单击按钮时出现此错误
System.Data.OleDb.OleDbException (0x80040E07): Data type mismatch in criteria expression.
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
这是我的代码
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);
}
}
}
错误提到的第35行包含
command.ExecuteNonQuery();
我在单击按钮时收到此错误,但 visual studio 中的代码调试完美且错误为 0。那么我在这里错过了什么地方?
您正在使用来自某个文本框的值,因此您需要使用它的 Text
属性 来获取值
command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2.Text + "', '" + ageTextBox2.Text + "', '" + gradeTextBox2.Text + "', '" + classTextBox2.Text + "') ";
我正在创建一个简单的程序,现在的想法是通过我的 C# 程序将数据 "Person information Name, Age ETC. " 插入数据库,当我单击按钮时出现此错误
System.Data.OleDb.OleDbException (0x80040E07): Data type mismatch in criteria expression. 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
这是我的代码
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);
}
}
}
错误提到的第35行包含
command.ExecuteNonQuery();
我在单击按钮时收到此错误,但 visual studio 中的代码调试完美且错误为 0。那么我在这里错过了什么地方?
您正在使用来自某个文本框的值,因此您需要使用它的 Text
属性 来获取值
command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2.Text + "', '" + ageTextBox2.Text + "', '" + gradeTextBox2.Text + "', '" + classTextBox2.Text + "') ";