C#插入访问数据库:无错误,命令成功,但未插入数据 [英] C# insert into access database: no error, command succeeds, but no data inserted

查看:373
本文介绍了C#插入访问数据库:无错误,命令成功,但未插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C#(在Visual Studio 2013上)与.Net 4.0和Access 2002数据库(.mdb)一起使用 我要插入的数据存储在以下变量中:

I'm using C# (on Visual Studio 2013) with .Net 4.0 and an access 2002 database (.mdb) The data I'm trying to insert is stored in these variables:

int cantidad = int.Parse(txtCantParejas.Text);
DateTime fechaActual = DateTime.Now;
int dia = fechaActual.Day;
int mes = fechaActual.Month;
int anio = fechaActual.Year;
int hora = fechaActual.Hour;
int minuto = fechaActual.Minute;

我已经建立了这个查询:

I've built this query:

string query = "INSERT INTO Torneo (cantParejas,dia,mes,anio,hora,minuto)VALUES (@cantParejas,@dia,@mes,@anio,@hora,@minuto)";

我有一个连接字符串(连接测试成功)存储在变量"strDeConexion"中.

And I have a connection string (connection test succeeds) stored in the variable "strDeConexion".

所以我尝试将数据插入到我的表中:

So I try to insert the data into my table:

OleDbConnection dbConnection = new OleDbConnection(strDeConexion);
OleDbCommand commandStatement = new OleDbCommand(query, dbConnection);
dbConnection.Open();
commandStatement.Parameters.Add("@cantParejas", OleDbType.Integer).Value = cantidad;
commandStatement.Parameters.Add("@dia", OleDbType.Integer).Value = dia;
commandStatement.Parameters.Add("@mes", OleDbType.Integer).Value = mes;
commandStatement.Parameters.Add("@anio", OleDbType.Integer).Value = anio;
commandStatement.Parameters.Add("@hora", OleDbType.Integer).Value = hora;
commandStatement.Parameters.Add("@minuto", OleDbType.Integer).Value = minuto;
commandStatement.ExecuteNonQuery();
dbConnection.Close();

我的Access数据库表中的列都是Integer类型,并且还有一个名为"idTorneo"的自动递增列,这是主键,我没有将其包括在查询中(因为它是自动递增的).

The columns in my Access database table are all of Integer type and there is also an autoincrementing column called "idTorneo" which is the primary key and I didn't include in my query (because it's auto increment).

但是,当我运行代码时,没有任何错误(我尝试将插入内容包装在try-catch中,并且看到"try"位正在执行).但是,当我检查表中的数据时,该表为空.由于我没有错误,所以我很困惑,也不知道从哪里开始寻找. 我也尝试过这种插入方式,但是没有运气:

However, when I run my code, I get no errors (I tried wrapping the insertion in a try-catch and I see the "try" bit being executed). But when I check the data in my table, the table is empty. Since I get no errors I'm quite puzzled and don't know where to start looking. I tried inserting this way as well, with no luck:

commandStatement.CommandType = CommandType.Text;
commandStatement.Parameters.AddWithValue("cantParejas", cantidad);
commandStatement.Parameters.AddWithValue("dia", dia);
commandStatement.Parameters.AddWithValue("mes", mes);
commandStatement.Parameters.AddWithValue("anio", anio);
commandStatement.Parameters.AddWithValue("hora", hora);
commandStatement.Parameters.AddWithValue("minuto", minuto);

对于可能出问题或如何调查此问题的任何帮助,将不胜感激:)

Any help on what could be wrong or how to investigate this issue will be appreciated :)

推荐答案

OleDbCommand不完全支持命名参数.使用?代替:

The OleDbCommand does not fully support named parameters. Use ? instead:

 string query = "INSERT INTO Torneo (cantParejas,dia,mes,anio,hora,minuto) VALUES (?, ?, ?, ?, ?, ?)";

这篇关于C#插入访问数据库:无错误,命令成功,但未插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆