单击一次按钮即可插入和更新

时间:2015-02-19 09:08:07

标签: c#

如何使我的代码下面插入处理表并从一个按钮点击删除问题表。平均时间只删除

connection.Open();

OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string con = "";
con = "delete from issue where Serial_Number ='" + serialNumber_txt.Text + " '";
con  = "insert into disposal (Serial_Number,Asset_Class,Asset_Code,Asset_Description,Model_Number,Engine_Number,PRB_Number,Quantity,From_dipo,Date_of_Dispo) values ('" + serialNumber_txt.Text + "','" + assetClass_txt.Text + "','" + assetCode_txt.Text + "','" + assetDesrip_txt.Text + "','" + modelNumber_txt.Text + "','" + engineNumber_txt.Text + "','" + prgNumber_txt.Text + "','" + quantity_txt.Text + "','" + from1.Text + "','" + this.dateTimePicker3.Text + "');";
command.CommandText = con;
command.ExecuteNonQuery();
MessageBox.Show("Disposal was successful");
from1.Clear();
quantity_txt.Clear();
engineNumber_txt.Clear();
serialNumber_txt.Clear();
modelNumber_txt.Clear();
assetDesrip_txt.Clear();
assetCode_txt.Clear();
assetClass_txt.Clear();
connection.Close();

2 个答案:

答案 0 :(得分:1)

每个查询需要有两个差异变量并执行两个:

query1 = "delete from issue where Serial_Number ='" + serialNumber_txt.Text + " '";
query2 = "insert into disposal (Serial_Number,Asset_Class,Asset_Code,Asset_Description,Model_Number,Engine_Number,PRB_Number,Quantity,From_dipo,Date_of_Dispo) values ('" + serialNumber_txt.Text + "','" + assetClass_txt.Text + "','" + assetCode_txt.Text + "','" + assetDesrip_txt.Text + "','" + modelNumber_txt.Text + "','" + engineNumber_txt.Text + "','" + prgNumber_txt.Text + "','" + quantity_txt.Text + "','" + from1.Text + "','" + this.dateTimePicker3.Text + "');";
command.CommandText = query1;
command.ExecuteNonQuery();
command.CommandText = query2;
command.ExecuteNonQuery();

也可以使用parameters作为评论,例如:

 query1 = "delete from issue where Serial_Number =@param";
 command.CommandText = query1;
 command.Parameters.AddwithValue("@param",serialNumber_txt.Text );

答案 1 :(得分:1)

如果您正在寻找一个不太复杂,更干燥(不要重复自己)的解决方案,请尝试以下方法:

定义执行多个查询的方法,如下所示:

public void executeQuery(string[] queries)
{
    connection.Open();

    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    foreach(string query in queries)
    {
      string con = query;
      command.CommandText = con;
      command.ExecuteNonQuery();
    }

    connection.close();
}

...现在,你可以这样做:

executeQuery(
    new string[] 
        {"delete from issue where Serial_Number ='" + serialNumber_txt.Text + " '", 
         "insert into disposal (Serial_Number,Asset_Class,Asset_Code,Asset_Description,Model_Number,Engine_Number,PRB_Number,Quantity,From_dipo,Date_of_Dispo) values ('" + serialNumber_txt.Text + "','" + assetClass_txt.Text + "','" + assetCode_txt.Text + "','" + assetDesrip_txt.Text + "','" + modelNumber_txt.Text + "','" + engineNumber_txt.Text + "','" + prgNumber_txt.Text + "','" + quantity_txt.Text + "','" + from1.Text + "','" + this.dateTimePicker3.Text + "');"
        }
    );