下面的插入数据库的操作已经屏蔽掉了,就单单读取近16000条数据就报错,使用文本分隔 还是一样报错!
int i = -1;
using (StreamReader m_streamreader1 = new StreamReader(FilePath, Encoding.GetEncoding("GB2312")))
{
i = m_streamreader1.ReadToEnd().Split('\n').Length - 1;//获取行数
}
string l_path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
int num = Convert.ToInt32(Math.Ceiling(i / 10000.00));//0x1869F0x82C_
for (int ai = 1; ai <= num; ai++)
{
int flag = 0;
string _path = l_path + "\\0x1869F0x82C_" + ai.ToString() + ".txt";
File.Delete(_path);
FileStream fs = new FileStream(_path, FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); StreamReader m_streamreader1 = new StreamReader(FilePath, Encoding.GetEncoding("GB2312")); while (!m_streamreader1.EndOfStream)
{
if (flag == 0)
{
string str = m_streamreader1.ReadLine();
sw.WriteLine(str);
}
else if (flag <= ai * 10000 && flag > (ai - 1) * 10000)
{
string str = m_streamreader1.ReadLine();
sw.WriteLine(str);
}
else
{
if ((num - ai + 1) * 10000 < flag)
{
m_streamreader1.ReadToEnd();
}
else
{
m_streamreader1.ReadLine();
}
}
flag++;
}
m_streamreader1.Close();
sw.Close();
fs.Close();
} DBHelper dbhelper = new DBHelper();
if (IsDel == 1)
{
dbhelper.IDM_Data("delete from GoodInfo");
} progressBar1.Maximum = i;//设置最大长度值
progressBar1.Value = 0;//设置当前值
int fuck = 0;
for (int bi = 1; bi <= num; bi++)
{
if (fuck == 12047)
{
string asdfasdf = "";
}
string _path = l_path + "\\0x1869F0x82C_" + bi.ToString() + ".txt";
FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.Read);
StreamReader m_streamreader = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
m_streamreader.BaseStream.Seek(0, SeekOrigin.Begin);
int k = 0;
int columnCount = 0;
SqlCeConnection con = new SqlCeConnection(dbhelper.CONN_STRING);
try
{
while (!m_streamreader.EndOfStream)
{
if (k == 0)
{
columnCount = m_streamreader.ReadLine().Split(',').Length;
if (columnCount < 6 || columnCount > 8)
{
MessageBox.Show("数据格式不正确,导入中断!", "错误"); break;
}
}
else
{
fuck++;
decimal a = 0m;
decimal b = 0m;
string[] line = m_streamreader.ReadLine().Split(',');
string txt = "";
string txm = "";
if (line[1].Length > 13)
{
txm = line[1].Substring(0, 13);
}
else
{
txm = line[1];
}
if (line.Length < 6 || line.Length > 8)
{
MessageBox.Show("数据格式不正确,导入中断!", "错误"); break;
}
//txt = "delete from GoodInfo where tiaoma='" + txm + "';";
if (columnCount == 7)
{
if (string.IsNullOrEmpty(line[6]))
{
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "',0);";
}
else
{
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "'," + line[6] + ");";
}
}
else if (columnCount == 6)
{
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "',0);";
}
else if (columnCount == 8)
{
if (string.IsNullOrEmpty(line[6]))
a = 0m;
else
a = Convert.ToDecimal(line[6]); if (string.IsNullOrEmpty(line[7]))
b = 0m;
else
b = Convert.ToDecimal(line[7]);
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia,lingshoujia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "'," + a + "," + b + ");";
}
else
{
MessageBox.Show("数据格式不正确,导入中断!", "错误"); break;
} if (!string.IsNullOrEmpty(txt))
{
try
{
//con.Open();
//SqlCeCommand cmd = con.CreateCommand();
//cmd.CommandText = txt;
//cmd.CommandType = CommandType.Text;
//int _mes = cmd.ExecuteNonQuery();
}
catch (Exception ee)
{
if (ee.Message.IndexOf("unique", 0) > 1 || ee.Message.IndexOf("uq", 0) > 1 || ee.Message.IndexOf("UQ", 0) > 1)
{
MessageBox.Show("条码:" + txm + "重复,导入终止!");
break;
}
}
progressBar1.Value += 1; }
}
k++;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, fuck.ToString() + "错误");
}
finally
{
con.Close();
}
con.Close();
//f.doReturnValue();
m_streamreader.Close();
fs.Close();
//File.Delete(_path);
}
this.Close();
int i = -1;
using (StreamReader m_streamreader1 = new StreamReader(FilePath, Encoding.GetEncoding("GB2312")))
{
i = m_streamreader1.ReadToEnd().Split('\n').Length - 1;//获取行数
}
string l_path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
int num = Convert.ToInt32(Math.Ceiling(i / 10000.00));//0x1869F0x82C_
for (int ai = 1; ai <= num; ai++)
{
int flag = 0;
string _path = l_path + "\\0x1869F0x82C_" + ai.ToString() + ".txt";
File.Delete(_path);
FileStream fs = new FileStream(_path, FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); StreamReader m_streamreader1 = new StreamReader(FilePath, Encoding.GetEncoding("GB2312")); while (!m_streamreader1.EndOfStream)
{
if (flag == 0)
{
string str = m_streamreader1.ReadLine();
sw.WriteLine(str);
}
else if (flag <= ai * 10000 && flag > (ai - 1) * 10000)
{
string str = m_streamreader1.ReadLine();
sw.WriteLine(str);
}
else
{
if ((num - ai + 1) * 10000 < flag)
{
m_streamreader1.ReadToEnd();
}
else
{
m_streamreader1.ReadLine();
}
}
flag++;
}
m_streamreader1.Close();
sw.Close();
fs.Close();
} DBHelper dbhelper = new DBHelper();
if (IsDel == 1)
{
dbhelper.IDM_Data("delete from GoodInfo");
} progressBar1.Maximum = i;//设置最大长度值
progressBar1.Value = 0;//设置当前值
int fuck = 0;
for (int bi = 1; bi <= num; bi++)
{
if (fuck == 12047)
{
string asdfasdf = "";
}
string _path = l_path + "\\0x1869F0x82C_" + bi.ToString() + ".txt";
FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.Read);
StreamReader m_streamreader = new StreamReader(fs, Encoding.GetEncoding("GB2312"));
m_streamreader.BaseStream.Seek(0, SeekOrigin.Begin);
int k = 0;
int columnCount = 0;
SqlCeConnection con = new SqlCeConnection(dbhelper.CONN_STRING);
try
{
while (!m_streamreader.EndOfStream)
{
if (k == 0)
{
columnCount = m_streamreader.ReadLine().Split(',').Length;
if (columnCount < 6 || columnCount > 8)
{
MessageBox.Show("数据格式不正确,导入中断!", "错误"); break;
}
}
else
{
fuck++;
decimal a = 0m;
decimal b = 0m;
string[] line = m_streamreader.ReadLine().Split(',');
string txt = "";
string txm = "";
if (line[1].Length > 13)
{
txm = line[1].Substring(0, 13);
}
else
{
txm = line[1];
}
if (line.Length < 6 || line.Length > 8)
{
MessageBox.Show("数据格式不正确,导入中断!", "错误"); break;
}
//txt = "delete from GoodInfo where tiaoma='" + txm + "';";
if (columnCount == 7)
{
if (string.IsNullOrEmpty(line[6]))
{
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "',0);";
}
else
{
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "'," + line[6] + ");";
}
}
else if (columnCount == 6)
{
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "',0);";
}
else if (columnCount == 8)
{
if (string.IsNullOrEmpty(line[6]))
a = 0m;
else
a = Convert.ToDecimal(line[6]); if (string.IsNullOrEmpty(line[7]))
b = 0m;
else
b = Convert.ToDecimal(line[7]);
txt += "insert into GoodInfo (tid,tiaoma,kuanhao,pinming,yanse,chicun,diaopaijia,lingshoujia) values (" + line[0] + ",'" + txm + "','" + line[2] + "','" + line[3] + "','" + line[4] + "','" + line[5] + "'," + a + "," + b + ");";
}
else
{
MessageBox.Show("数据格式不正确,导入中断!", "错误"); break;
} if (!string.IsNullOrEmpty(txt))
{
try
{
//con.Open();
//SqlCeCommand cmd = con.CreateCommand();
//cmd.CommandText = txt;
//cmd.CommandType = CommandType.Text;
//int _mes = cmd.ExecuteNonQuery();
}
catch (Exception ee)
{
if (ee.Message.IndexOf("unique", 0) > 1 || ee.Message.IndexOf("uq", 0) > 1 || ee.Message.IndexOf("UQ", 0) > 1)
{
MessageBox.Show("条码:" + txm + "重复,导入终止!");
break;
}
}
progressBar1.Value += 1; }
}
k++;
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, fuck.ToString() + "错误");
}
finally
{
con.Close();
}
con.Close();
//f.doReturnValue();
m_streamreader.Close();
fs.Close();
//File.Delete(_path);
}
this.Close();
string txt = "";
string txm = "";
if (line[1].Length > 13)
{
txm = line[1].Substring(0, 13);
}
else
{
txm = line[1];
}这里 你确定 line 里有值 且大于等于2个么