以下为报错内容:
由于将在索引、 主关键字、或关系中创建重复的值,请求对表的改变没有成功。 改变该字段中的或包含重复数据的字段中的数据,删除索引或重新定义索引以允许重复的值并再试一次。原因我知道是因为写入了重复数据报错,我就想如果出现这个错误,我把重复的内容显示出来,然后接着执行循环
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public OleDbConnection GreatConn()
        {
            OleDbConnection objConnection = new OleDbConnection("provider=Microsoft.jet.OLEDB.4.0;data Source=E:/VB/data.Mdb");
            return objConnection;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            int L = 0;
           
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.FilterIndex = 1;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {                FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
                StreamReader m_streamReader = new StreamReader(fs);
                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);                string strLine = m_streamReader.ReadLine();                OleDbConnection conn = GreatConn();
                conn.Open();
                while (strLine != null)
                {
                    L++;
                    Console.WriteLine("{0}", strLine);
                    string dt = DateTime.Now.ToString();
                    string tjcomm = "insert into [table](imei) values ('" + strLine + "')";                    OleDbCommand comm = new OleDbCommand(tjcomm, conn);
                    comm.ExecuteNonQuery();
                    comm.Dispose();                    richTextBox1.AppendText(strLine + "\n");
                    strLine = m_streamReader.ReadLine();                }
                conn.Close();
                m_streamReader.Close();
                fs.Close();
                string message = "导入数量总计:" + L;
                MessageBox(message);
            }        }        private void MessageBox(string message)
        {
            throw new NotImplementedException();
        }               private void Form1_Load(object sender, EventArgs e)
        {        }
    }
}

解决方案 »

  1.   

    这个工具的主要目的就是不要导入重复的IMEI号,原数据是txt文件,导入数据库的时候需要验证导入的数据是否有重复,我就直接在access里面把IMEI字段设置了唯一。我就是想如果出现重复报错,显示重复的内容,然后接着导数据,有其它方法也行。
      

  2.   

    抓一下while的异常把L和strLine throw出来不就看见第多少行的数据了吗!
    然后continue让他继续循环List<int,>
    try{
    while (strLine != null)                
    {                    
    L++;                    
    Console.WriteLine("{0}", strLine);
    string dt = DateTime.Now.ToString();                   
    string tjcomm = "insert into [table](imei) values ('" + strLine + "')";                     
    OleDbCommand comm = new OleDbCommand(tjcomm, conn);                    comm.ExecuteNonQuery();                    
    comm.Dispose();                     
    richTextBox1.AppendText(strLine + "\n");                    
    strLine = m_streamReader.ReadLine();                 
    }
    }catche(Exception e)
    {
    MessageBox(L+1+"行"+strLine );
    continue;
    }