我无法让我的 mySQL 删除命令工作

Posted

技术标签:

【中文标题】我无法让我的 mySQL 删除命令工作【英文标题】:I cant get my mySQL Delete command to work 【发布时间】:2020-06-11 20:22:09 【问题描述】:

see my previous question as reference.

这样我就可以将行插入到我的 mysql 数据库中。 现在我正在尝试从数据库中删除行

我将我的代码减少到最低限度应该可以工作:

您需要引用 MySql.Data.MySqlClient 并使用 .Net 4.5.2

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace ***_mySQLTest

    class Program
    
        private static string server = "ds2";
        private static string database = "fhem";
        private static string user = "fhemdbuser";
        private static string passwort = "!2345Abcde";
        static void Main(string[] args)
        
            MySqlCommand cmd;
            MySqlTransaction trans;

            MySqlConnection connection;

            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            builder.Server = server;
            builder.Database = database;
            builder.UserID = user;
            builder.Port = 3307;
            builder.Password = passwort;
            builder.DefaultCommandTimeout = 120;
            builder.UseDefaultCommandTimeoutForEF = true;
            connection = new MySqlConnection(builder.ConnectionString);


            // Datenbank öffnen
            connection.Open();


            MySqlCommand selectCmdCurrentTest = new MySqlCommand("select `TIMESTAMP`, `DEVICE`, `TYPE`, `EVENT`, `READING`, `VALUE`, `UNIT` from currentTest;", connection);
            MySqlDataAdapter dataAdapterCurrentTest = new MySqlDataAdapter(selectCmdCurrentTest);
            MySqlCommand updateCmd = new MySqlCommand("update `currentTest` set `TIMESTAMP`= @p1,`DEVICE`= @p2,`TYPE`= @p3,`EVENT`= @p4,`READING`= @p5,`VALUE`= @p6,`UNIT`= @p7", connection);
            MySqlParameter p1, p2, p3, p4, p5, p6, p7;

            p1 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Timestamp", ParameterName = "@p1", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.DateTime;
            p2 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Device", ParameterName = "@p2", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString;
            p3 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Type", ParameterName = "@p3", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString;
            p4 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Event", ParameterName = "@p4", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString;
            p5 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Reading", ParameterName = "@p5", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString;
            p6 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Value", ParameterName = "@p6", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString;
            p7 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Unit", ParameterName = "@p7", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString;

            updateCmd.Parameters.AddRange(new[]  p1, p2, p3, p4, p5, p6, p7 );
            dataAdapterCurrentTest.UpdateCommand = updateCmd;
            MySqlCommand deleteCmd = new MySqlCommand("delete from  `currentTest` where `TIMESTAMP`like @p1 and `DEVICE` like @p2 and `TYPE` like  @p3 and `EVENT` like  @p4 and `READING` like  @p5 and `VALUE` like  @p6 and `UNIT` like  @p7", connection);
            p1 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Timestamp", ParameterName = "@p1", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.DateTime, SourceVersion = DataRowVersion.Original ;
            p2 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Device", ParameterName = "@p2", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p3 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Type", ParameterName = "@p3", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p4 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Event", ParameterName = "@p4", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p5 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Reading", ParameterName = "@p5", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p6 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Value", ParameterName = "@p6", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p7 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Unit", ParameterName = "@p7", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;

            deleteCmd.Parameters.AddRange(new[]  p1, p2, p3, p4, p5, p6, p7 );
            dataAdapterCurrentTest.DeleteCommand = deleteCmd;

            DataSet ds = new DataSet();
            dataAdapterCurrentTest.Fill(ds, "currentTest");

这里ds中的表还是空的


            for (int i = 0; i < 10; i++)
            

                DataRow newrow = ds.Tables["currentTest"].NewRow();
                newrow["Timestamp"] = DateTime.Now;
                newrow["Device"] = "Device" + DateTime.Now.Millisecond.ToString();
                newrow["TYPE"] = "Type" + DateTime.Now.Millisecond.ToString();
                newrow["EVENT"] = "Event" + DateTime.Now.Millisecond.ToString();
                newrow["READING"] = "Reading" + DateTime.Now.Millisecond.ToString();
                newrow["VALUE"] = "Value" + DateTime.Now.Millisecond.ToString();
                newrow["UNIT"] = "Unit" + DateTime.Now.Millisecond.ToString();
                ds.Tables["currentTest"].Rows.Add(newrow);
            

            new MySqlCommandBuilder(dataAdapterCurrentTest);

            dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
            ds.Tables["currentTest"].AcceptChanges();

这里ds 有 10 行包含预期数据。

            for (int i = 0; i < ds.Tables["currentTest"].Rows.Count; i++)
            
                ds.Tables["currentTest"].Rows[i]["Timestamp"] = new DateTime(2020, 7, 1, 0, 0, 0);
            
            new MySqlCommandBuilder(dataAdapterCurrentTest);
            dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
            ds.Tables["currentTest"].AcceptChanges();

不过,ds 有 10 行包含预期数据(与以前相同,但日期设置为 2020-07-01


            for (int i = 0; i < ds.Tables["currentTest"].Rows.Count; i++)
            
                ds.Tables["currentTest"].Rows[i].Delete();
            
            new MySqlCommandBuilder(dataAdapterCurrentTest);
            dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);

上面的行抛出异常“System.Data.DBConcurrencyException”,并带有消息“Parallelitätsverletzung: Der DeleteCommand hat sich auf 0 der erwarteten 1 Datensätze ausgewirkt.”,这意味着 1 个预期行中有 0 个已受到影响

            ds.Tables["currentTest"].AcceptChanges();
        
    

我不明白如何创建删除命令以使其正常工作

【问题讨论】:

不管怎样,您的数据访问策略真的已经过时了。考虑改用 Entity Framework Core 或像 Dapper 这样的微 ORM。 我想你可以在这里找到答案https://***.com/questions/3388100/concurrency-violation-the-updatecommand-affected-0-of-the-expected-1-records @RobertHarvey:如果它很容易,我会使用 EFC。将 EF 与 MSSQL 或 LocalDB 一起使用是可行的,但我无法让它与 mySQL 一起使用。所以我被困在这里。但考虑到我实现的目的,它仍然可以完成工作。 @Ramin,我读过这个,但是你一次删除一个数据行,那不是非常低效吗?我想有一个使用 DataSets、DataTables 和 DataRows 来处理我的数据的实现。 Dapper 适用于任何实现DbConnection、including MySql 的数据库。 【参考方案1】:

好的, 我自己找到了解决方案。 问题似乎是您必须用数据库中的数据再次填充数据集...... 所以这里是额外的代码部分:

DataSet ds = new DataSet();之前:

            MySqlParameter p1, p2, p3, p4, p5, p6, p7;

            MySqlCommand deleteCmd = new MySqlCommand("delete from  `currentTest` where `TIMESTAMP`like @p1 and `DEVICE` like @p2 and `TYPE` like  @p3 and `EVENT` like  @p4 and `READING` like  @p5 and `VALUE` like  @p6 and `UNIT` like  @p7", conn);
            p1 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Timestamp", ParameterName = "@p1", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.DateTime, SourceVersion = DataRowVersion.Original ;
            p2 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Device", ParameterName = "@p2", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p3 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Type", ParameterName = "@p3", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p4 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Event", ParameterName = "@p4", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p5 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Reading", ParameterName = "@p5", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p6 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Value", ParameterName = "@p6", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;
            p7 = new MySql.Data.MySqlClient.MySqlParameter()  SourceColumn = "Unit", ParameterName = "@p7", MySqlDbType = MySql.Data.MySqlClient.MySqlDbType.VarString, SourceVersion = DataRowVersion.Original ;

            deleteCmd.Parameters.AddRange(new[]  p1, p2, p3, p4, p5, p6, p7 );
            deleteCmd.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;
            dataAdapterCurrentTest.DeleteCommand = deleteCmd;

添加行之后:

            new MySqlCommandBuilder(dataAdapterCurrentTest);

            dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
            ds.Tables["currentTest"].AcceptChanges();
            ds.Tables["currentTest"].Clear();
            dataAdapterCurrentTest.Fill(ds, "currentTest");
            for (int i = 0; i < 20; i++)
            
                ds.Tables["currentTest"].Rows[i].Delete();
            
            new MySqlCommandBuilder(dataAdapterCurrentTest);

            dataAdapterCurrentTest.Update(ds.Tables["currentTest"]);
            ds.Tables["currentTest"].AcceptChanges();
            ds.Tables["currentTest"].Clear();
            dataAdapterCurrentTest.Fill(ds, "currentTest");

【讨论】:

以上是关于我无法让我的 mySQL 删除命令工作的主要内容,如果未能解决你的问题,请参考以下文章

如何让我的机器人消息响应特定频道中的命令

我无法让我的新机器人“imagesearch”工作,正在寻求帮助

Discord.py - 如果命令被编辑,让我的机器人编辑他的响应

MySQL 工作台中的交互式命令行

如何更新mysql数据库中的数千行

如何让我的 Node.js MySQL 连接作为承诺工作?