ms access数据库中标准表达式异常中数据类型不匹配

本文关键字:异常 数据类型 不匹配 表达式 标准 access 数据库 ms | 更新日期: 2023-09-27 18:13:21

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

System.Data.OleDb。OleDbException (0x80040E07):标准表达式中的数据类型不匹配。在System.Data.OleDb.OleDbCommand。ExecuteCommandTextErrorHandling (OleDbHResult人力资源)在System.Data.OleDb.OleDbCommand。ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object&executeResult)在System.Data.OleDb.OleDbCommand.ExecuteCommandText (Object&executeResult)在System.Data.OleDb.OleDbCommand。执行命令(CommandBehavior行为,对象&executeResult)在System.Data.OleDb.OleDbCommand。ExecuteReaderInternal(CommandBehavior行为,字符串方法)在System.Data.OleDb.OleDbCommand.ExecuteNonQuery ()在School_System.newRegisteration。' users ' omars_000 'documents'visual studio 2015'Projects'School System'School System' newregistration .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个错误。这里我漏了什么位置呢

ms access数据库中标准表达式异常中数据类型不匹配

你正在使用一些文本框的值,所以你需要使用它的Text属性来获取值

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