捕获重复条目 OleDbException 并覆盖

Catch Duplicate Entry OleDbException & Over-write

我在尝试向我的 Access 数据库中添加行时处理条目时遇到此错误。问题是我正在处理的某些条目是唯一的,有些条目可能具有重复的主键并更新了相关列。但是,由于有些具有重复的主键,因此处理的所有内容都会失败。

我想用新数据覆盖重复条目并添加新条目。我该怎么做?

我在下面做了一些测试,试图找出问题的一部分,使用 try / catch 块解决了所有问题,但无济于事:

            using (OleDbConnection myCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Lenovo\Desktop\Weather Cruncher\wcMainDB.accdb"))
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandText = "Insert into stations_NOAA " +
                    "([stationID],[stationName],[stationMinDate],[stationMaxDate],[stationDataCoverage]," +
                    "[stationLatitude],[stationLongitude],[stationElevation],[stationElevationUnit])" +
                    "values (?,?,?,?,?,?,?,?,?)";
                cmd.Connection = myCon;
                myCon.Open();

                // Populate Access Database
                foreach (Station loc in noaa.results)
                {
                    try
                    {
                        cmd.Parameters.AddWithValue("@stationID", loc.id); // Short Text data type
                        cmd.Parameters.AddWithValue("@stationName", loc.name); // Short Text data type
                        cmd.Parameters.AddWithValue("@stationMinDate", loc.mindate); // Short Text data type
                        cmd.Parameters.AddWithValue("@stationMaxDate", loc.maxdate); // Short Text data type
                        cmd.Parameters.AddWithValue("@stationDataCoverage", loc.datacoverage); // Number data type
                        cmd.Parameters.AddWithValue("@stationLatitude", loc.latitude); // Number data type
                        cmd.Parameters.AddWithValue("@stationLongitude", loc.longitude); // Number data type
                        cmd.Parameters.AddWithValue("@stationElevation", loc.elevation); // Number data type
                        cmd.Parameters.AddWithValue("@stationElevationUnit", loc.elevationUnit); // Short Text data type
                        cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                    }
                    catch(Exception ex)
                    {
                        if (ex.Message.Contains("duplicate values")) // It is a primary key violation, ignore
                        {
                            Console.WriteLine("Dupe skipped.");
                            continue;
                        }
                        else
                        {
                            MessageBox.Show(ex.ToString() + "\n\n" + ex.Message);
                        }
                    }
                }
            }

它正在处理 20 个查询,其中 15 个是唯一的。在将主键与程序正在处理的数据进行比较时,我逐步观看了它。如何解决这两个问题?

也许可以考虑这样的事情...假设 Station_ID 是一个唯一字段...

foreach (Station loc in noaa.results)
{
    cmdCount.CommandText = "SELECT count(*) from stations_NOAA WHERE StationId = @station_id";
    cmdCount.Parameters.AddWithValue("@station_id", loc.id);
    int count = (int)cmdCount.ExecuteScalar();

    if (count > 0)
    {
         // UPDATE STATEMENT
         cmd.CommandText "UPDATE stations_NOAA Set stationMinDate = @stationMinDate, Set StationMaxDate = @stationMaxDate, ...
             where StationID = @stationID;"
        cmd.Parameters.AddWithValue("@stationID", loc.id); // Short Text data type
        cmd.Parameters.AddWithValue("@stationMinDate", loc.mindate); // Short Text data type
        cmd.Parameters.AddWithValue("@stationMaxDate", loc.maxdate); // Short Text data type
        ...
        ...
        cmd.ExecuteNonQuery();
    }
    else
    {
         // INSERT STATEMENT
         commandText = ("Insert into stations_NOAA " +
                        "([stationID],[stationName],[stationMinDate],[stationMaxDate],[stationDataCoverage]," +
                        "[stationLatitude],[stationLongitude],[stationElevation],[stationElevationUnit])" +
                        "values (?,?,?,?,?,?,?,?,?)";
         cmd.Parameters.AddWithValue("@stationID", loc.id); // Short Text data type
        cmd.Parameters.AddWithValue("@stationMinDate", loc.mindate); // Short Text data type
        cmd.Parameters.AddWithValue("@stationMaxDate", loc.maxdate); // Short Text data type
        ...
        ...
        cmd.ExecuteNonQuery();
    }
    cmd.Parameters.Clear();
}

如果更新和插入中的所有参数都相同,则此模式:

foreach (Station loc in noaa.results)
{
    cmd.CommandText = "SELECT count(*) from stations_NOAA WHERE StationId = @station_id", myConnection);
    cmdCount.Parameters.AddWithValue("@station_id", loc.id);
    int count = (int)cmdCount.ExecuteScalar();

    if (count > 0)
        cmd.CommandText "UPDATE stations_NOAA " _
            "Set stationMinDate = @stationMinDate, " _
            "Set StationMaxDate = @ StationMaxDate, " _
               ...
            "where StationID = @stationID;"

    else
        commandText = "Insert into stations_NOAA " +
                        "([stationID],[stationName],[stationMinDate],[stationMaxDate],[stationDataCoverage]," +
                        "[stationLatitude],[stationLongitude],[stationElevation],[stationElevationUnit])" +
                        "values (@stationID,@stationMinDate, @ ...)";

    cmd.Parameters.AddWithValue("@stationID", loc.id); // Short Text data type
    cmd.Parameters.AddWithValue("@stationMinDate", loc.mindate); // Short Text data type
    cmd.Parameters.AddWithValue("@stationMaxDate", loc.maxdate); // Short Text data type
    ...
    ...

    cmd.ExecuteNonQuery();      

    cmd.Parameters.Clear();
}

但这只有在 Insert 和 Update 具有相同的字段并且需要大量阅读 cleaner/easier 时才有效。