C# 当我再次选择同一个文件时不起作用:索引超出了数组错误的界限

C# 当我再次选择同一个文件时不起作用:索引超出了数组错误的界限,c#,winforms,excel,C#,Winforms,Excel,这是我第一次选择这个文件,它工作得很好。当我再次选择同一个文件时,它给出了更新的值:索引超出了数组错误的范围。你知道哪里出错了吗?我该如何做到这一点?我使用相同的excel文件更新数据。 以下是代码: lblDisplay.Text = string.Empty; if (radSite.Checked==true) { if (openFileDialog.ShowDialog(this) == DialogResult.OK)

这是我第一次选择这个文件,它工作得很好。当我再次选择同一个文件时,它给出了更新的值:索引超出了数组错误的范围。你知道哪里出错了吗?我该如何做到这一点?我使用相同的excel文件更新数据。 以下是代码:

  lblDisplay.Text = string.Empty;
   if (radSite.Checked==true)
            {
                if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    path = openFileDialog.InitialDirectory + openFileDialog.FileName;
                }
                string constring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + path + "; Extended Properties=Excel 12.0 Xml";
                OleDbConnection con = new OleDbConnection(constring);
                try
                {
                    con.Open();
                    dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    String[] excelSheets = new String[dt.Rows.Count];
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[i] = row["TABLE_NAME"].ToString(); //am getting error at this point
                        i++;
                    }

                    for (int j = 0; j < excelSheets.Length; j++)
                    {
                        string query = "SELECT * FROM [" + excelSheets[j] + "]; ";
                        OleDbDataAdapter adp = new OleDbDataAdapter(query, constring);

                        DataSet ds = new DataSet();
                        string sCode = string.Empty;
                        string sdescription = string.Empty;
                        adp.Fill(ds);

                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            sCode = Convert.ToString(dr[1]);
                            sdescription = Convert.ToString(dr[2]);
                            // if (!(string.IsNullOrEmpty(hCode) && string.IsNullOrEmpty(description)))
                            {

                                dboject.InsertSiteNameIntoDatabase(sCode, sdescription);

                            }
                        }


                    }
                    lblDisplay.Text = "Inserted successfully";
                }
                catch (Exception ex)
                {
                    dboject.VMSLog(ex.Message);

                }
                finally
                {
                    con.Close();

                }

            }
lblDisplay.Text=string.Empty;
if(radSite.Checked==true)
{
if(openFileDialog.ShowDialog(this)=DialogResult.OK)
{
path=openFileDialog.InitialDirectory+openFileDialog.FileName;
}
string constring=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+path+”“扩展属性=Excel 12.0 Xml”;
OLEDB连接con=新OLEDB连接(构造);
尝试
{
con.Open();
dt=con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
String[]excelSheets=新字符串[dt.Rows.Count];
foreach(数据行中的数据行)
{
excelSheets[i]=行[“TABLE_NAME”]。ToString();//此时出现错误
i++;
}
对于(int j=0;j
您正在访问计数器
i
上的数组
excelSheets
,您可能在程序开始时初始化
i
一次,然后您没有在代码中将其重置回
0
。这就是为什么你会得到例外。在代码中,定义数组
excelSheets
,然后将
i
设置为
0
如下:

String[] excelSheets = new String[dt.Rows.Count];
i = 0; //here
//then rest of your code
foreach (DataRow row in dt.Rows)
{
    excelSheets[i] = row["TABLE_NAME"].ToString(); 
    i++;
}

是的,在发布问题后,我理解了我的错误。。。thanks@kida,不客气,还记得在迭代中使用不同的计数器变量,这将使代码更易于阅读和调试