插入数据表或更新(如果存在)

insert into datatable or update if it exist

我正在尝试更新 1 个数据 table 并将其插入另一个数据。如果数据 table(c2 条形码) 中的工单相同,它将更新数据 table(c1 条形码),如果不相同,它将插入数据 table来自数据 table c2 条码的 c1 条码。我能够 运行 代码和插入部分,它正确地插入到数据库中,但对于更新部分,它不起作用。我的更新或条件有误吗?

connection.Open();
OleDbCommand checkrecord = new OleDbCommand("SELECT workorder FROM [c1 barcode]", connection);
OleDbCommand checkrecord2 = new OleDbCommand("SELECT workorder FROM [c2 barcode]", connection);
if (checkrecord == checkrecord2)
{
    string query = "UPDATE [c1 barcode], [c2 barcode] SET [c1 barcode].[Close from care] = [c2 barcode].[close from care], [c1 barcode].[Name care] = [c2 barcode].[name care] WHERE ([c1 barcode].Workorder=[c2 barcode].[workorder]);";
    using (OleDbCommand cmd = new OleDbCommand(query, connection))
    {
        cmd.ExecuteNonQuery();
        MessageBox.Show("updated");
    }
}
else if (checkrecord != checkrecord2)
{
    string query2 = "INSERT INTO [c1 barcode] SELECT [c2 barcode].* FROM [c2 barcode]";
    using (OleDbCommand cmd = new OleDbCommand(query2, connection))
    {
        cmd.ExecuteNonQuery();
        MessageBox.Show("inserted");
    }
}
else
{
    MessageBox.Show("error");
}
connection.Close();

checkrecord == checkrecord2 是错误的,你只是在比较两个 OleDbCommand 对象,我猜你有两个执行这两个命令然后比较这些命令的输出

像这样:

//psuedo code
connection.Open();
OleDbCommand cmd_checkrecord = new OleDbCommand("SELECT workorder FROM [c1 barcode]", connection);
OleDbCommand cmd_checkrecord2 = new OleDbCommand("SELECT workorder FROM [c2 barcode]", connection);
var checkrecord = Convert.ToString(cmd_checkrecord.ExecuteScalar());
var checkrecord2 = Convert.ToString(cmd_checkrecord2.ExecuteScalar());

if (checkrecord == checkrecord2)
{
    string query = "UPDATE [c1 barcode], [c2 barcode] SET [c1 barcode].[Close from care] = [c2 barcode].[close from care], [c1 barcode].[Name care] = [c2 barcode].[name care] WHERE ([c1 barcode].Workorder=[c2 barcode].[workorder]);";
    using (OleDbCommand cmd = new OleDbCommand(query, connection))
    {
        cmd.ExecuteNonQuery();
        MessageBox.Show("updated");
    }
}
else if (checkrecord != checkrecord2)
{
    string query2 = "INSERT INTO [c1 barcode] SELECT [c2 barcode].* FROM [c2 barcode]";
    using (OleDbCommand cmd = new OleDbCommand(query2, connection))
    {

        cmd.ExecuteNonQuery();

        MessageBox.Show("inserted");
    }
}
else
{
    MessageBox.Show("error");
}
connection.Close();

您不需要先运行检查。如果您只需 运行 两个 SQL 语句,它将起作用,如下所示:

connection.Open();
string updateQuery = "UPDATE [c1 barcode], [c2 barcode] SET [c1 barcode].[Close from care] = [c2 barcode].[close from care], [c1 barcode].[Name care] = [c2 barcode].[name care] WHERE ([c1 barcode].Workorder=[c2 barcode].[workorder]);";
string insertQuery = "INSERT INTO [c1 barcode] SELECT * FROM [c2 barcode] c2 WHERE NOT EXISTS (SELECT 1 FROM [c1 barcode] WHERE Workorder = c2.Workorder)";
using (OleDbCommand cmd = new OleDbCommand(updateQuery, connection))
{
    if ((int)cmd.ExecuteNonQuery() > 0)
    {
        MessageBox.Show("updated");
    }
}
using (OleDbCommand cmd = new OleDbCommand(insertQuery, connection))
{
    if ((int)cmd.ExecuteNonQuery() > 0)
    {
        MessageBox.Show("inserted");
    }
}
connection.Close();

这假设 Workorder 是主键,我认为这是正确的,因为你的插入语句。