无法在Microsoft Access数据库中插入数据[C#]



我有一个访问文件,该文件具有6个字段:

ID, Designation, Price, Group, Weight, DeliveryTime

在我的代码中,我首先在数据库类中创建了一个OLEDBCONNECTION和OLEDBCOMMAND。

private OleDbConnection _connection = new OleDbConnection(parameter);
private OleDbCommand _command;

之后,我执行了写入方法。

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

我忽略了什么吗?

感谢您的帮助!

我添加了一个新的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");
}

似乎您必须创建一个新命令并执行它,因为它现在正在工作。

感谢您在评论中的帮助。

相关内容

  • 没有找到相关文章

最新更新