[英]Unable to insert Data inside Microsoft Access Database [C#]
I have an Access File that has 6 fields like this: 我有一个具有以下6个字段的访问文件:
ID, Designation, Price, Group, Weight, DeliveryTime
In my code, I first created an OleDbConnection and an OleDbCommand inside my Database Class. 在我的代码中,我首先在数据库类内部创建了一个OleDbConnection和一个OleDbCommand。
private OleDbConnection _connection = new OleDbConnection(parameter);
private OleDbCommand _command;
After that I execute the WriteData Method. 之后,我执行WriteData方法。
public void WriteData(string tableName, string designation, string price, string group, string weight, string deliveryTime)
{
_command.Connection = _connection;
_command.CommandText = "INSERT INTO " +
tableName + " (ID, Designation, Price, Group, Weight, deliveryTime)
VALUES (@id, @des, @price, @group, @weight, @deliveryTime)";
_command.Parameters.AddWithValue("@id", GetNextHighestItemNumber());
_command.Parameters.AddWithValue("@des", designation);
_command.Parameters.AddWithValue("@price", price);
_command.Parameters.AddWithValue("@group", group);
_command.Parameters.AddWithValue("@weight", weight);
_command.Parameters.AddWithValue("@deliveryTime", deliveryTime);
OpenConnection();
_command.ExecuteNonQuery();
CloseConnection();
System.Windows.Forms.MessageBox.Show("Success");
}
But there is no new Data inside my Database. 但是我的数据库中没有新数据。 I also tried it this way:
我也这样尝试过:
public void WriteData(string tableName, string designation, string price, string group, string weight, string deliveryTime)
{
_command.Connection = _connection;
_command.CommandText = "INSERT INTO " +
tableName + " (ID, Designation, Price, Group, Weight, deliveryTime)
VALUES (?,?,?,?,?,?)";
_command.Parameters.AddWithValue("ID", GetNextHighestItemNumber());
_command.Parameters.AddWithValue("Designation", designation);
_command.Parameters.AddWithValue("Price", price);
_command.Parameters.AddWithValue("Group", group);
_command.Parameters.AddWithValue("Weight", weight);
_command.Parameters.AddWithValue("DeliveryTime", deliveryTime);
OpenConnection();
_command.ExecuteNonQuery();
CloseConnection();
System.Windows.Forms.MessageBox.Show("Success");
}
Do I overlook something? 我会忽略什么吗?
Thanks for your help! 谢谢你的帮助!
I added a NEW OleDbCommand and applied the connection, command, and Parameters to it. 我添加了一个新的OleDbCommand并将连接,命令和参数应用到它。
public void WriteData(string tableName, string designation, string price, string group, string weight, string deliveryTime)
{
_command.CommandText = "INSERT INTO " +
tableName + " (ID, Designation, Price, Group, Weight, deliveryTime)
VALUES (?,?,?,?,?,?)";
OleDbCommand addDataCommand = new OleDbCommand(_command.CommandText, _connection);
addDataCommand .Parameters.AddWithValue("ID", GetNextHighestItemNumber());
addDataCommand .Parameters.AddWithValue("Designation", designation);
addDataCommand .Parameters.AddWithValue("Price", price);
addDataCommand .Parameters.AddWithValue("Group", group);
addDataCommand .Parameters.AddWithValue("Weight", weight);
addDataCommand .Parameters.AddWithValue("DeliveryTime", deliveryTime);
OpenConnection();
_command.ExecuteNonQuery();
CloseConnection();
System.Windows.Forms.MessageBox.Show("Success");
}
It seems that you have to create a new Command and execute it because it's working now. 似乎您必须创建一个新命令并执行它,因为它现在正在工作。
Thanks for your help in the comments. 感谢您在评论中的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.