我已经尝试过搜索教程,但似乎没有一个能让它工作,因为现在我的删除按钮没有从数据库中删除的代码。我将添加您解决问题所需的任何详细信息。
我的添加代码:
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();
}
}
中截取的截图
答案 0 :(得分:0)
实际上,您将执行与insert命令相同的过程。唯一的问题是,您需要能够唯一地标识要删除的条目。我不确定 tbl_Assets 的结构,但我认为它确实有一个唯一的 ID 字段,您可以在插入后立即检索。获取,记住(存储在memtable中)然后使用您唯一的 ID 是实现的主要部分,然后您可以使用 AssetID 删除。
从设计的角度来看,我不会使用默认的OleDbDataAdapter,而是创建自己的,并将所有重要且经常使用的查询放在那里。所以这将是一种可能的解决方案:
{
...
// 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);
}
这样你应该有一个干净的解决方案和一个只能在一个地方定义一次查询的可共享对象。但是典型的微软编码......你的代码不断增长和发展,最终你拥有一个不可撼动的怪物。
请注意我已经写了这篇文章,这是未经测试的,但是应该运行或推动你正确的方式。