简体   繁体   English

C#插入访问数据库:无错误,命令成功,但未插入数据

[英]C# insert into access database: no error, command succeeds, but no data inserted

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: 我正在将C#(在Visual Studio 2013上)与.Net 4.0和Access 2002数据库(.mdb)配合使用我要插入的数据存储在以下变量中:

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

And I have a connection string (connection test succeeds) stored in the variable "strDeConexion". 我有一个存储在变量“ 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();

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). 我的Access数据库表中的列都是Integer类型的,还有一个名为“ idTorneo”的自动递增列,这是主键,我没有将其包括在查询中(因为它是自动递增的)。

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). 但是,当我运行代码时,没有收到任何错误(我尝试将插入内容包装在try-catch中,并且看到“ try”位正在执行)。 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 :) 任何关于错误或如何调查此问题的帮助将不胜感激:)

The OleDbCommand does not fully support named parameters. OleDbCommand不完全支持命名参数。 Use ? 使用? instead: 代替:

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

Well, I found the "bug" and it was just a configuration issue I would've never imagined. 好吧,我找到了“错误”,这只是我从未想过的配置问题。 I tried everything suggested here (thanks!!) and I finally decided to avoid parameters to see if that was the problem. 我尝试了这里建议的所有内容(谢谢!!),最后我决定避免使用参数来查看是否是问题所在。 So I converted my query to "INSERT INTO Torneo (cantParejas,dia,mes,anio,hora,minuto) VALUES ('" + cantidad + "','" + dia + "','" + mes + "','" + anio + "','" + hora + "','" + minuto + "')"; 所以我将查询转换为"INSERT INTO Torneo (cantParejas,dia,mes,anio,hora,minuto) VALUES ('" + cantidad + "','" + dia + "','" + mes + "','" + anio + "','" + hora + "','" + minuto + "')"; and left the code just as 并像

OleDbConnection dbConnection = new OleDbConnection();
dbConnection.ConnectionString = strDeConexion;
dbConnection.Open();
OleDbCommand commandStatement = new OleDbCommand();
commandStatement.Connection = dbConnection;
commandStatement.CommandText = query;
commandStatement.ExecuteNonQuery();
dbConnection.Close();

However, I still was getting nothing. 但是,我仍然一无所获。 Until I went to my Solution Explorer, selected my database and changed the property "Copy to output directory" from "Copy always" to "Do not copy", then went to the output directory (....\\bin\\debug) and manually pasted my database file in there. 在转到解决方案资源管理器之前,选择我的数据库并将“复制到输出目录”属性从“始终复制”更改为“不复制”,然后转到输出目录(.... \\ bin \\ debug),然后手动将我的数据库文件粘贴到那里。 Ta-da! 当当! Now THIS copy of the database is getting updated. 现在,此数据库副本正在更新。

I could've spent the whole week debugging without even thinking of this little trick, so I wanted to post it in here in case anyone else has the same problem. 我可能花了整整一个星期的调试时间,甚至没有想到这个小技巧,因此我想将其发布在这里,以防其他人遇到相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM