将数据发送到 Access 数据库时出错
Error sending data to the Access Database
当我尝试将我的代码发送到 Access 数据库时出现此异常:
System.Data.OleDb.OleDbException (0x80040E07): data type mismatch in a selection condition 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 KPIZ.Form2.btn_save_Click(Object sender, EventArgs e) in C:\Users\booku\source\repos\KPIZ\KPIZ\Form2.cs:line 110
我的代码:
private void btn_save_Click(object sender, EventArgs e)
{
try
{
byte[] imageBt = null;
FileStream fstream = new FileStream(this.textBox2.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = @"INSERT INTO [SportInfo]([ID],[FirstName],[LastName],[DOB],[Sport],[DIW],[Image])" + "VALUES(@id, @Fn, @Ln, @Date, @Sports, @days ,@IMG)";
command.Parameters.Add(new OleDbParameter("@Fn", Convert.ToString(txt_fname.Text)));
command.Parameters.Add(new OleDbParameter("@Ln", Convert.ToString(txt_lname.Text)));
command.Parameters.Add(new OleDbParameter("@Date", Convert.ToString(mtxt_dob.Text)));
command.Parameters.Add(new OleDbParameter("@Sports", Convert.ToString(txt_sport.Text)));
command.Parameters.Add(new OleDbParameter("@days", Convert.ToInt32(nmc_diw.Text)));
command.Parameters.Add(new OleDbParameter("@id", Convert.ToInt32(txt_id.Text)));
command.Parameters.Add(new OleDbParameter("@IMG", imageBt));
command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unlucky " + ex);
connection.Close();
}
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select ID,LastName,FirstName,DOB,Sport,DIW, from SportInfo ";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();
}
}
OLEDB 参数始终是位置性的。这意味着查询中参数的顺序需要与代码中添加参数的顺序相匹配。
此外,您需要将日期参数添加为日期,而不是字符串。
调整您的参数顺序以匹配:
command.Parameters.Add(new OleDbParameter("@id", Convert.ToInt32(txt_id.Text)));
command.Parameters.Add(new OleDbParameter("@Fn", Convert.ToString(txt_fname.Text)));
command.Parameters.Add(new OleDbParameter("@Ln", Convert.ToString(txt_lname.Text)));
command.Parameters.Add(new OleDbParameter("@Date", DateTime.Parse(mtxt_dob.Text)));
command.Parameters.Add(new OleDbParameter("@Sports", Convert.ToString(txt_sport.Text)));
command.Parameters.Add(new OleDbParameter("@days", Convert.ToInt32(nmc_diw.Text)));
command.Parameters.Add(new OleDbParameter("@IMG", imageBt));
如果顺序不匹配,则会出现很多错误,因为现在您正试图在日期列或整数列中插入文本字段。
当我尝试将我的代码发送到 Access 数据库时出现此异常:
System.Data.OleDb.OleDbException (0x80040E07): data type mismatch in a selection condition 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 KPIZ.Form2.btn_save_Click(Object sender, EventArgs e) in C:\Users\booku\source\repos\KPIZ\KPIZ\Form2.cs:line 110
我的代码:
private void btn_save_Click(object sender, EventArgs e)
{
try
{
byte[] imageBt = null;
FileStream fstream = new FileStream(this.textBox2.Text, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fstream);
imageBt = br.ReadBytes((int)fstream.Length);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = @"INSERT INTO [SportInfo]([ID],[FirstName],[LastName],[DOB],[Sport],[DIW],[Image])" + "VALUES(@id, @Fn, @Ln, @Date, @Sports, @days ,@IMG)";
command.Parameters.Add(new OleDbParameter("@Fn", Convert.ToString(txt_fname.Text)));
command.Parameters.Add(new OleDbParameter("@Ln", Convert.ToString(txt_lname.Text)));
command.Parameters.Add(new OleDbParameter("@Date", Convert.ToString(mtxt_dob.Text)));
command.Parameters.Add(new OleDbParameter("@Sports", Convert.ToString(txt_sport.Text)));
command.Parameters.Add(new OleDbParameter("@days", Convert.ToInt32(nmc_diw.Text)));
command.Parameters.Add(new OleDbParameter("@id", Convert.ToInt32(txt_id.Text)));
command.Parameters.Add(new OleDbParameter("@IMG", imageBt));
command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unlucky " + ex);
connection.Close();
}
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select ID,LastName,FirstName,DOB,Sport,DIW, from SportInfo ";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();
}
}
OLEDB 参数始终是位置性的。这意味着查询中参数的顺序需要与代码中添加参数的顺序相匹配。
此外,您需要将日期参数添加为日期,而不是字符串。
调整您的参数顺序以匹配:
command.Parameters.Add(new OleDbParameter("@id", Convert.ToInt32(txt_id.Text)));
command.Parameters.Add(new OleDbParameter("@Fn", Convert.ToString(txt_fname.Text)));
command.Parameters.Add(new OleDbParameter("@Ln", Convert.ToString(txt_lname.Text)));
command.Parameters.Add(new OleDbParameter("@Date", DateTime.Parse(mtxt_dob.Text)));
command.Parameters.Add(new OleDbParameter("@Sports", Convert.ToString(txt_sport.Text)));
command.Parameters.Add(new OleDbParameter("@days", Convert.ToInt32(nmc_diw.Text)));
command.Parameters.Add(new OleDbParameter("@IMG", imageBt));
如果顺序不匹配,则会出现很多错误,因为现在您正试图在日期列或整数列中插入文本字段。