首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

大神帮忙见见,老是报"IndexOutOfRangeException"

2012-09-27 
大神帮忙看看,老是报IndexOutOfRangeException下面的插入数据库的操作已经屏蔽掉了,就单单读取近16000条

大神帮忙看看,老是报"IndexOutOfRangeException"
下面的插入数据库的操作已经屏蔽掉了,就单单读取近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();

[解决办法]
索引超出了范围了,。

找到哪行报错的,


[解决办法]
IndexOutOfRangeException
一般是
数组使用的时候,下标超过数组长度
或者字符串
Substring之类的,超过了字符串的长度

等等

类似这样的原因导致的,

不能调试,你可以先注释掉一些行,进行测试,看在哪注释的地方出错的

比如
 string[] line = m_streamreader.ReadLine().Split(',');

你使用之前,应该判断line.Length>6你才能使用line[6]
等等

[解决办法]
错误信息 发出来呗..
出错在哪一行
[解决办法]
索引超出了范围了,首先看看有数组或者别的有关索引问题,然后慢慢调试看超出的地方···
(代码太多了,都懒得看)

热点排行