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:
我正在使用带有.Net 4.0的C#(在Visual Studio 2013上)和访问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). 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:
但是,当我运行我的代码时,我没有错误(我尝试将插入包装在try-catch中,我看到“try”位正在执行)。但是当我检查表格中的数据时,表格是空的。由于我没有错误,我很困惑,不知道从哪里开始寻找。我尝试插入这种方式,没有运气:
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 :)
任何有关错误或如何调查此问题的帮助将不胜感激:)
2 个解决方案
#1
The OleDbCommand does not fully support named parameters. Use ?
instead:
OleDbCommand不完全支持命名参数。使用 ?代替:
string query = "INSERT INTO Torneo (cantParejas,dia,mes,anio,hora,minuto) VALUES (?, ?, ?, ?, ?, ?)";
#2
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 + "')";
and left the code just as
好吧,我发现了“bug”,这只是一个我从未想象过的配置问题。我尝试了这里建议的一切(谢谢!!),我最终决定避免参数,看看是不是问题。所以我将我的查询转换为“INSERT INTO Torneo(cantParejas,dia,mes,anio,hora,minuto)VALUES('”+ cantidad +“','”+ dia +“','”+ mes +“',' “+ anio +”','“+ hora +”','“+ minuto +”')“;并将代码保留为
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. Ta-da! Now THIS copy of the database is getting updated.
但是,我仍然一无所获。在我进入我的解决方案资源管理器之前,选择了我的数据库并将“复制到输出目录”属性从“始终复制”更改为“不复制”,然后转到输出目录(.... \ bin \ debug)并且手动粘贴我的数据库文件。当当!现在,该数据库副本正在更新。
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.
我可以整整一个星期调试,甚至没有想到这个小技巧,所以我想在这里发布它,以防其他人有同样的问题。
#1
The OleDbCommand does not fully support named parameters. Use ?
instead:
OleDbCommand不完全支持命名参数。使用 ?代替:
string query = "INSERT INTO Torneo (cantParejas,dia,mes,anio,hora,minuto) VALUES (?, ?, ?, ?, ?, ?)";
#2
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 + "')";
and left the code just as
好吧,我发现了“bug”,这只是一个我从未想象过的配置问题。我尝试了这里建议的一切(谢谢!!),我最终决定避免参数,看看是不是问题。所以我将我的查询转换为“INSERT INTO Torneo(cantParejas,dia,mes,anio,hora,minuto)VALUES('”+ cantidad +“','”+ dia +“','”+ mes +“',' “+ anio +”','“+ hora +”','“+ minuto +”')“;并将代码保留为
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. Ta-da! Now THIS copy of the database is getting updated.
但是,我仍然一无所获。在我进入我的解决方案资源管理器之前,选择了我的数据库并将“复制到输出目录”属性从“始终复制”更改为“不复制”,然后转到输出目录(.... \ bin \ debug)并且手动粘贴我的数据库文件。当当!现在,该数据库副本正在更新。
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.
我可以整整一个星期调试,甚至没有想到这个小技巧,所以我想在这里发布它,以防其他人有同样的问题。