-3

I'm using .net, C# , LINQ, SQL Server 2012.

this work basically just insert many data into DB.

I found i missed about 7,000 rows over total 85,000 rows.

Then i go debug , i found there are some data with real duplicate key .

But most of their key is no existed in DB and the DB also return error as duplicate key.

any ideas please?

code is like this

    try
    {
        resCnt++;
        dbconn.table.AddObject(newRow);
        dbconn.SaveChanges();

    }
    catch (Exception ex)
    {
        Console.WriteLine("DB fail ID:"+ newRow.id);
        List< table> testRepeat=(
            from da in dbconn.table
            where da.id==table.id
            select da
            ).Take(1).ToList();

        if (testRepeat.Count() > 0)
        {
            Console.WriteLine(DateTime.Now+ "Repeated ID: " + table.id+" Saved at "+testRepeat.First().LastModified);
            repeatCnt++;
        }
        else
        {
            Console.WriteLine("!!terrible!!");
        }

there are some ids: 322476,438095 .... the fist 322476 is exised , so the error say "322476 duplicate key " as usual

but when i get 438095 to dbconn.SaveChanges(); the 438095 is no existed it will go to the exception part the inner error still saying "322476 duplicate key... " which at least should be "438095 duplicate key... "

then i click the variable row.id , it is 438095 ...

4

1 回答 1

0

感谢大家。

我找到了一个解决方案,那就是当我第一次发现重复键错误出现时。这意味着真正的重复键。

我通过以下方式重新建立数据库连接:

                dbconn.Dispose();
                dbconn = new Entities();

那么这将起作用

多谢!希望这对其他人有帮助

于 2013-10-15T21:46:52.643 回答