简体   繁体   English

C#MS Access数据库如何删除

[英]C# MS Access database how to delete

I've tried searching for tutorials and none of them seems to get it to work, as of now my Delete button has no code for deleting from the database. 我曾尝试搜索教程,但似乎没有一个能正常工作,因为到目前为止,我的“删除”按钮没有用于从数据库中删除的代码。 I'll add any details you need for the problem. 我将添加您需要解决该问题的所有详细信息。

My code for adding: 我添加的代码:

private void btnSaveAddAsset_Click(object sender, EventArgs e)
{
        if (txtAddFloor.Text == "" || txtAddRoom.Text == "" || string.IsNullOrWhiteSpace(txtAddFloor.Text) == true || string.IsNullOrWhiteSpace(txtAddRoom.Text) == true)
        {
            MessageBox.Show("Please enter valid information", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
        }
        else
        {
            con = new OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source =AssetManagement.accdb");
            Ds = new DataSet();


            ths.lstViewListOfRooms.Items.Add(txtAddFloor.Text).SubItems.Add(txtAddRoom.Text);

            String date = "dd/MM/yyyy - HH:mm:ss";
            ths.lstViewListOfRooms.Items[ths.lstViewListOfRooms.Items.Count - 1].SubItems.Add(txtAddDescriptionDetail.Text);
            ths.lstViewListOfRooms.Items[ths.lstViewListOfRooms.Items.Count - 1].SubItems.Add(DateTime.Now.ToString(date));


            string query = "INSERT INTO tbl_Assets(asset_floor, asset_room, asset_description, asset_createdOn)" + " VALUES (" + txtAddFloor.Text + "," + txtAddRoom.Text + ", '" + txtAddDescriptionDetail.Text + "' , '" + DateTime.Now.ToString(date) + "'" + ") ";
            con.Open();
            Da = new OleDbDataAdapter(query, con);
            Da.Fill(Ds, "tbl_Assets");
            con.Close();
            this.Close();
        }
}

Screenshots I took from my previous question Error in displaying MS Access Database in C# 我从上一个问题中获取的屏幕截图在C#中显示MS Access数据库时出错

Actually you would do the same procedure as in your insert command. 实际上,您将执行与插入命令相同的过程。 The only problem is, that you would need to be able to uniquely identify the entry you want to delete. 唯一的问题是,您将需要能够唯一标识要删除的条目。 I am not sure about the structure of your tbl_Assets but i would assume, that it does have a unique ID field, that you can retrieve right after your insert. 我不确定您的tbl_Assets的结构,但我认为它确实具有唯一的ID字段,您可以在插入后立即进行检索。 Getting, remembering (storing in memtable) and then using your unique IDs , is the main part to achieve, then you can just delete using your AssetID . 获取,记住(存储在memtable中)然后使用唯一的ID是要实现的主要部分,然后可以使用AssetID删除。

From the design point, i would not use the default OleDbDataAdapter, but rather create your own, and place all important and often used queries in there. 从设计的角度来看,我不会使用默认的OleDbDataAdapter,而是创建自己的OleDbDataAdapter,并将所有重要且经常使用的查询放在其中。 So this would be one possible solution: 因此,这可能是一种解决方案:

{
    ...

    // open db connection
    con.Open();

    // get our own asset adapter
    Da = new AssetAdapter(con);

    // create a DataTable and fill it.
    DataTable assets = new DataTable();
    Da.Fill(assets);

    // add a RowDeleted event handler for the table.
    assets.RowDeleted += new DataRowChangeEventHandler(row_deleted);

    . . . you could define handlers for update and insert as well . . .

    // insert a test record
    DataRow assetRow = assets.NewRow();
    assetRow["asset_floor"] = "15";
    assetRow["asset_room"] = "7c";

    . . . and so on . . .

    assets.Rows.Add(assetRow);

    // update database.
    Da.Update(assetRow);

    // now get the asset id of the new created asset
    Int32 assetID = (Int32)Da.InsertCommand.Parameters["@Identity"].Value;

    // now we can use this id to delete the row again
    DataRow found = assets.Rows.Find(assetID);

    if (found != null) 
    {
        // now delete row again
        assets.Rows.Delete(assetID);
        Da.Update(assets)
    }

    con.Close();

    this.Close();

    ...

}

/**
* AssetAdapter returns the database adapter for selecting and deleting
*/
public static OleDbDataAdapter AssetAdapter(OleDbConnection connection)
{
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
    OleDbCommand command;
    OleDbParameter parameter;

    /*
     * Here we create a command that selects assets by
     * floor and room. You can add more fields if needed.
     * Finally the command is set as the adapters *SelectCommand*
     */

    command = new OleDbCommand("SELECT asset_id FROM tbl_Assets " +
        "WHERE asset_floor = ? AND asset_room = ?", connection);

    // add the parameters floor and room
    // (assuming VarChar is your field type, change accordingly) 
    command.Parameters.Add("asset_floor", OleDbType.VarChar, 20);
    command.Parameters.Add("asset_room", OleDbType.VarChar, 20);

    // finally set the command to the adapters select command
    dataAdapter.SelectCommand = command;


    /*
     * Now we create a command that inserts assets by
     * filling the fields needed.
     * Then the command is set as the adapters *InsertCommand*
     */

    // Create the InsertCommand.
    command = new OleDbCommand(
        "INSERT INTO tbl_Assets (asset_floor, asset_room, asset_description, asset_createdOn) " +
        "VALUES (?, ?, ?, ?)", connection);

    // add the parameters
    command.Parameters.Add(
        "asset_floor", OleDbType.VarChar, 20, "asset_floor");
    command.Parameters.Add(
        "asset_room", OleDbType.VarChar, 20, "asset_room");
    command.Parameters.Add(
        "asset_description", OleDbType.VarChar, 128, "asset_description");
    command.Parameters.Add(
        "asset_createdOn", OleDbType.DateTime, 20, "asset_createdOn");

    // create an output parameter for the new identity value.
    parameter = command.Parameters.Add("@Identity", SqlDbType.Int, 0, "asset_id");
    parameter.Direction = ParameterDirection.Output;

    // now we set the command to the adapters insert
    adapter.InsertCommand = command;

    . . .

        // same procedure for UpdateCommand

    . . .


    /*
     * Here we create a command that deletes assets by
     * asset_id. You can add more fields if needed.
     * Finally the command is set as the adapters *DeleteCommand*
     */

    // Create the DeleteCommand.
    command = new OleDbCommand(
        "DELETE * FROM tbl_Assets WHERE asset_id = ?", 
        connection);

    parameter = command.Parameters.Add(
        "asset_id", OleDbType.Char, 5, "asset_id");
    parameter.SourceVersion = DataRowVersion.Original;

    dataAdapter.DeleteCommand = command;

    return dataAdapter;
}

// event handler which is called when row is deleted
private static void row_deleted(object sender, DataRowChangeEventArgs e)
{
    Console.WriteLine("Row was DELETED event: name={0}/{1}; action={2}", 
        e.Row["asset_floor", DataRowVersion.Original], 
        e.Row["asset_room", DataRowVersion.Original], 
        e.Action);
}

This way you should have a clean solution and a shareable object which only defines queries once at one place. 这样,您应该有一个干净的解决方案和一个可共享的对象,该对象只在一处定义一次查询。 But typical Microsoft coding... your code keeps growing and growing and in the end you have an untamable monster. 但是典型的Microsoft编码...您的代码不断增长,最终您遇到了一个顽固的怪物。

BEWARE i have written this out of my head, it is untested, but is should pretty much run or push you the right way. 注意 ,这是我脑海中写出来的,未经测试,但是应该运行或以正确的方式推动您。

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

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