我有一个包含6个字段的访问文件:
ID, Designation, Price, Group, Weight, DeliveryTime
在我的代码中,我首先在我的数据库类中创建了一个OleDbConnection和一个OleDbCommand。
private OleDbConnection _connection = new OleDbConnection(parameter);
private OleDbCommand _command;
之后我执行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");
}
但我的数据库中没有新的数据。我也是这样试过的:
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");
}
我忽视了什么吗?
感谢您的帮助!
答案 0 :(得分:0)
我添加了一个新的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");
}
似乎你必须创建一个新命令并执行它,因为它现在正在工作。
感谢您在评论中的帮助。