[英]Data type mismatch in criteria expression exception in ms acess database
我現在正在創建一個簡單的程序,這個想法是通過c#程序在數據庫中插入數據“ Person information Name,Age ETC.”,當我單擊按鈕時,出現此錯誤
System.Data.OleDb.OleDbException(0x80040E07):條件表達式中的數據類型不匹配。 在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行
這是我的代碼
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 + "') ";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.