简体   繁体   English

如何从表中删除一行并将其通过SQL语句插入另一行?

[英]How can I remove one row from a table and insert it into another via a SQL statement?

My SQL statement works fine without the delete statement which is my problem. 没有删除语句,这是我的问题,我的SQL语句工作正常。 I use 2 OleDbCommands to call the seperate queries. 我使用2个OleDbCommands调用单独的查询。 This is my current SQL statement in C#: 这是我当前在C#中使用的SQL语句:

string mysql = "Insert Into Completed([Batch number], [Product], [Weight]) VALUES('" + batchNumber + "','" + product + "','" + weight + "')"; 

string newsql = "DELETE FROM Dryers WHERE [Batch number]='"+batchNumber+"'"; 字符串newsql =“从干燥机中删除[批次号] ='” + batchNumber +“'”;

This my latest failed attempt in code: 这是我最近在代码中失败的尝试:

        if (conn.State == ConnectionState.Open)
        {

            try
            {
                using (System.Transactions.TransactionScope tScope = new TransactionScope())
                {
                    string batchNumber = txtBatchNumber3.Text.ToString();
                    string product = txtProduct3.Text.ToString();
                    string weight = txtWeight3.Text.ToString();

                    string mysql = "Insert Into Packing([Batch number], [Product], [Weight]) VALUES('" + batchNumber + "','" + product + "','" + weight + "') ";
                    string newsql = "DELETE * FROM Dryers WHERE [Batch number]='" + batchNumber + "'";

                    OleDbCommand cmd = new OleDbCommand(mysql, conn);
                    cmd.ExecuteNonQuery();
                    OleDbCommand cmd2 = new OleDbCommand(newsql, conn);
                    cmd2.ExecuteNonQuery();

                    tScope.Complete();

                }


                MessageBox.Show("Data saved successfuly...!");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed due to" + ex.Message);
            }
            finally
            {
                conn.Close();                    
            }
            }                
        else
        {
            MessageBox.Show("Connection Failed");
        }

    }
    }
}

VS2012 gives the following error: VS2012出现以下错误:

Failed due toData type mismatch in criteria expression. 由于条件表达式中的数据类型不匹配而失败。

You can't do this in Access. 您无法在Access中执行此操作。 You have to call two separate SQL strings. 您必须调用两个单独的SQL字符串。

Use two statements but wrap them in a transaction so it's a single unit of work. 使用两个语句,但是将它们包装在一个事务中,因此它是一个工作单元。

BEGIN TRANSACTION
COMMIT

if you wanted to do it in C# then you would wrap your queries in a transaction scope. 如果您想使用C#进行操作,则可以将查询包装在事务范围中。

using (System.Transactions.TransactionScope tScope = new TransactionScope())
{
    //do work here
    //if work is completed
    tScope.Complete();

    //else do nothing the using statement will dispose the scope and rollback your changes.
}

Here is a walkthrough from Microsoft about making the connection against an access database and querying using ADO.NET. 这是Microsoft的演练,内容涉及如何针对访问数据库建立连接并使用ADO.NET查询。 https://msdn.microsoft.com/en-us/library/ms971485.aspx https://msdn.microsoft.com/en-us/library/ms971485.aspx

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

相关问题 如何使用Linq-to-SQL将一个表中的所有行插入到另一个表中? - How can I insert all rows from one table into another table with Linq-to-SQL? 如何使用 Linq to SQL 将一个表中的一行插入到另一个表中 - How to insert a row from one table to another table using Linq to SQL 如何根据另一个表删除一个表中的行? - How do I remove a row in one table based on another table? 如何从一个网格视图传输和删除单个选定的行到另一个网格视图 - how can i transfer and remove single selected row from one grid view to another 将数据从一个SQL表行插入到字段匹配的另一行-Entity Framework - Insert data from one sql table row to another where field matches - Entity Framework 从一张表中读取并插入到另一张中-一次一行 - Read from one table and insert into another - one row at a time 如何将行从一个数据库中的表移到另一个数据库? - How do I move a row from a table in one database to another? 我可以在没有往返的情况下将Select语句的结果插入另一个表吗? - Can I Insert the Results of a Select Statement Into Another Table Without a Roundtrip? 如何将一张表的id设置到另一张表 - How can i set the id from one table to another table 我想使用 SQL Server 中的存储过程将数据从一个表同时插入到另一个表中 - I want to insert data from one table into another at same time using stored procedure in SQL server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM