C# ms acess数据库中条件表达式异常中的数据类型不匹配

C# ms acess数据库中条件表达式异常中的数据类型不匹配,c#,visual-studio,ms-access,oledb,C#,Visual Studio,Ms Access,Oledb,我现在正在创建一个简单的程序,其想法是通过我的c#程序在数据库中插入数据“个人信息姓名、年龄等”,当我点击按钮时,我得到了这个错误 System.Data.OleDb.OLEDBEException(0x80040E07):条件表达式中的数据类型不匹配。 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)中 位于System.Data.OleDb.OleDbCommand.Execute

我现在正在创建一个简单的程序,其想法是通过我的c#程序在数据库中插入数据“个人信息姓名、年龄等”,当我点击按钮时,我得到了这个错误

System.Data.OleDb.OLEDBEException(0x80040E07):条件表达式中的数据类型不匹配。 在System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)中 位于System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,Object&ExecuteSult) 在System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&ExecuteSult)中 位于System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior,Object&executeResult) 位于System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior,String方法) 位于System.Data.OleDb.OleDbCommand.ExecuteNonQuery()处 在School\u System.NewRegistration.button1\u单击C:\Users\OmarS\u 000\documents\visual studio 2015\Projects\School System\School System\NewRegistration.cs:第35行中的(对象发送方,事件参数e)

这是我的密码

   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();

当我单击按钮时会出现此错误,但是VisualStudio中的代码调试完美,没有错误。那么我在这里错过了什么呢?

您正在使用某个文本框中的值,因此需要使用其
Text
属性来获取值

command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2.Text + "', '" + ageTextBox2.Text + "', '" + gradeTextBox2.Text + "', '" + classTextBox2.Text + "') ";

您正在使用某个文本框中的值,因此需要使用其
Text
属性来获取值

command.CommandText = "INSERT into School ([Name], [Age], [Grade], [Class]) VALUES('" + nameTextBox2.Text + "', '" + ageTextBox2.Text + "', '" + gradeTextBox2.Text + "', '" + classTextBox2.Text + "') ";

当您为数字或日期字段传递文本时,将发生这种情况。使用SQL参数为数字或日期字段传递文本时将发生的情况。使用SQL参数非常感谢你,我不知道我怎么会错过它。工作时出现0个错误:D非常感谢你,我不知道我怎么会错过这些。已处理0个错误:D