以下为报错内容:
由于将在索引、 主关键字、或关系中创建重复的值,请求对表的改变没有成功。 改变该字段中的或包含重复数据的字段中的数据,删除索引或重新定义索引以允许重复的值并再试一次。原因我知道是因为写入了重复数据报错,我就想如果出现这个错误,我把重复的内容显示出来,然后接着执行循环
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)
{ }
}
}
由于将在索引、 主关键字、或关系中创建重复的值,请求对表的改变没有成功。 改变该字段中的或包含重复数据的字段中的数据,删除索引或重新定义索引以允许重复的值并再试一次。原因我知道是因为写入了重复数据报错,我就想如果出现这个错误,我把重复的内容显示出来,然后接着执行循环
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)
{ }
}
}
解决方案 »
- 行15: 无法获取属性“appendChild”的值: 对象为 null 或未定义
- C# 有没有给指定的句柄发送消息
- 请问GDI+中有那些参数的设置可以提高Drawimage函数的速度啊?
- using System.NET无法手动添加到代码中......
- 节假日提个小问题
- 方法返回一个DataSet的传递问题
- datagridivew每行单元格没底线了……
- 不简单的问题!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 如何监视、取得windows的打印队列的文件信息?
- 求C语言程序填空
- C#用system.io.directory.getfiles查找文件出错的问题
- 为何捕获不到button控件的mouseup事件?
然后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;
}