【发布时间】:2016-10-16 18:38:20
【问题描述】:
我正在创建一个简单的程序,现在的想法是通过我的 c# 程序在数据库中插入数据“Person information Name, Age ETC.”,当我单击按钮时出现此错误
System.Data.OleDb.OleDbException (0x80040E07):条件表达式中的数据类型不匹配。 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult 小时) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams,对象和 executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(对象和执行结果) 在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior 行为,对象和 executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior 行为,字符串方法) 在 System.Data.OleDb.OleDbCommand.ExecuteNonQuery() 在 C:\Users\OmarS_000\documents\visual studio 2015\Projects\School System\School System\newRegisteration.cs:line 35 中的 School_System.newRegisteration.button1_Click(Object sender, EventArgs 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();
单击按钮时出现此错误,但 Visual Studio 中的代码完美调试,出现 0 个错误。那我错过了什么地方?
【问题讨论】:
-
当您为数字或日期字段传递文本时会发生这种情况。使用 SQL 参数
标签: c# visual-studio ms-access oledb