C# 如何抑制MySql.Data.MySqlClient.MySqlException重复条目

C# 如何抑制MySql.Data.MySqlClient.MySqlException重复条目,c#,mysql,error-handling,C#,Mysql,Error Handling,因此,我有一个数据库,它具有多个列的唯一约束。显然是为了防止重复。在调试我的应用程序时,我的控制台中绝对充满了MySql.Data.MySqlClient.MySqlExceptions 由于这是数据库的预期功能,是否有办法抑制这些功能?捕捉异常似乎并不能做到这一点。它将根据我的catch(MySqlException e)语句处理它,但仍然在控制台中显示MySql.Data.MySqlClient.MySqlException 这是我的密码: public void InsertNewTeam

因此,我有一个数据库,它具有多个列的唯一约束。显然是为了防止重复。在调试我的应用程序时,我的控制台中绝对充满了
MySql.Data.MySqlClient.MySqlException
s

由于这是数据库的预期功能,是否有办法抑制这些功能?捕捉异常似乎并不能做到这一点。它将根据我的
catch(MySqlException e)
语句处理它,但仍然在控制台中显示
MySql.Data.MySqlClient.MySqlException

这是我的密码:

public void InsertNewTeamHistory(TeamHistoryRow teamHistoryRow)
    {
        //open connection
        if (this.OpenConnection)
        {
            try
            {
                var query =
                    "INSERT INTO teamhistory(MatchURL, Date, TeamOne, TeamOneScore, TeamTwo, TeamTwoScore, Map, Event, Outcome) " +
                    "VALUES(?MatchURL, ?Date, ?TeamOne, ?TeamOneScore, ?TeamTwo, ?TeamTwoScore, ?Map, ?Event, ?Outcome)";

                //create command and assign the query and connection from the constructor
                using (var cmd = new MySqlCommand(query, _connection))
                {
                    if (cmd.Connection.State == ConnectionState.Open)
                    {
                        cmd.Parameters.Add("?MatchURL", MySqlDbType.VarChar).Value = teamHistoryRow.MatchUrl;
                        cmd.Parameters.Add("?Date", MySqlDbType.Date).Value = teamHistoryRow.Date;
                        cmd.Parameters.Add("?TeamOne", MySqlDbType.VarChar).Value = teamHistoryRow.TeamOne;
                        cmd.Parameters.Add("?TeamOneScore", MySqlDbType.Int32).Value = teamHistoryRow.TeamOneScore;
                        cmd.Parameters.Add("?TeamTwo", MySqlDbType.VarChar).Value = teamHistoryRow.TeamTwo;
                        cmd.Parameters.Add("?TeamTwoScore", MySqlDbType.Int32).Value = teamHistoryRow.TeamTwoScore;
                        cmd.Parameters.Add("?Map", MySqlDbType.VarChar).Value = teamHistoryRow.Map;
                        cmd.Parameters.Add("?Event", MySqlDbType.VarChar).Value = teamHistoryRow.Event;
                        cmd.Parameters.Add("?Outcome", MySqlDbType.VarChar).Value = teamHistoryRow.Outcome;

                        //Execute command
                        cmd.ExecuteNonQuery();

                        //close connection
                        this.CloseConnection();
                    }
                    else
                    {
                        throw new Exception("Failed - Connection to the DB NOT open.");
                    }
                }
            }
            catch (MySqlException e)
            {
                switch (e.Number)
                {
                    case 1062:
                        Console.WriteLine("Found Duplicate TeamHistoryRow, Not Inserting.");
                        break;
                    case 0:
                        Console.WriteLine("Well, we fucked up.");
                        break;
                    default:
                        throw;
                }
            }
            finally
            {
                try
                {
                    this.CloseConnection();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
    }

MySql有一个好用且易于使用的特性,它为您的语句添加了一条规则,说明在找到重复键时该怎么做

INSERT INTO ..... ON DUPLICATE KEY UPDATE .....
因此,您的查询语句可以重写为

INSERT INTO teamhistory(MatchURL, Date, TeamOne, TeamOneScore, TeamTwo, 
                       TeamTwoScore, Map, Event, Outcome) 
                       VALUES(?MatchURL, ?Date, ?TeamOne, 
                       ?TeamOneScore, ?TeamTwo, ?TeamTwoScore, 
                       ?Map, ?Event, ?Outcome) 
ON DUPLICATE KEY UPDATE MatchUrl = ?MatchUrl
这假定字段MatchUrl是触发重复异常的密钥。因此,当检测到重复键时,sql将执行更新,而不会更改表中的任何内容


通过标准化的

在其他数据库系统中也可以使用相同的逻辑,如果记录已经存在,您想做什么?您想用这些值更新该记录,还是忽略插入并继续?忽略插入并继续。但是我不想让它在MySql.Data.dll中输出抛出的异常:'MySql.Data.MySqlClient.MySqlException',因为我每分钟处理数千行或数千行,这使得读取我的实际程序输出非常困难。太棒了,先生!非常感谢你。这会导致MySql
cmd
成功完成,而不会出错。美丽的!我选择了你的答案作为最佳答案!