索引阵列超出范围的异常的(indexing Array out of Range Exception

2019-10-17 17:37发布

我尝试保存一个数组中的SQL结果,然后返回。 但我发现了异常:数组超出范围的错误。

这里是我的代码:

 public BookingUpdate[] getBookingUpdates(string token)
{
    String command = "SELECT b.ID,b.VERANSTALTER, rr.VON ,rr.BIS, b.THEMA, b.STORNO, ra.BEZEICHNUNG from BUCHUNG b JOIN RESERVIERUNGRAUM rr on rr.BUCHUNG_ID = b.ID JOIN RAUM ra on ra.ID = rr.RAUM_ID WHERE b.UPDATE_DATE BETWEEN DATEADD (DAY , -20 , getdate()) AND getdate() AND b.BOOKVERNR = 0";
    SqlConnection connection = new SqlConnection(GetConnectionString());
    BookingUpdate[] bookingupdate = new BookingUpdate[1];
    connection.Open();
    try
    {
        SqlCommand cmd = new SqlCommand(command, connection);
        SqlDataReader rdr = null;
        int count = 0;
        rdr = cmd.ExecuteReader();


            while (rdr.Read())
            {
                DataTable dt = new DataTable();
                dt.Load(rdr);
                count = dt.Rows.Count;
                for (int c = 0; c < count; c++)
                {
                    bookingupdate = new BookingUpdate[c];
                    bookingupdate[c].bookingID = (long)rdr["ID"]; // <---- Error is here
                    bookingupdate[c].fullUserName = rdr["VERANSTALTER"].ToString();
                    bookingupdate[c].newStart = (DateTime)rdr["VON"];
                    bookingupdate[c].newStart = (DateTime)rdr["BIS"];
                    bookingupdate[c].newSubject = rdr["THEMA"].ToString();
                    bookingupdate[c].newlocation = rdr["BEZEICHNUNG"].ToString();
                    if (rdr["STORNO"].ToString() != null)
                    {
                        bookingupdate[c].deleted = true;
                    }
                    else
                    {
                        bookingupdate[c].deleted = false;
                    }

                }

            }

    }

    catch (Exception ex)
    {
        log.Error(ex.Message + "\n\rStackTrace:\n\r" + ex.StackTrace);
    }
    finally
    {
        connection.Close();
    }
    return bookingupdate;
}

我在想什么?

Answer 1:

你似乎是创建和使用数组分配内存

bookingupdate = new BookingUpdate[c];

但实际上没有创造BookingUpdate的实例。 当您尝试您的数组元素上设置的属性没有实际BookingUpdate更新 - 只有一个持有人。

我建议改变你的代码的线沿线的东西:

...
bookingupdate = new BookingUpdate[count];  // allocates space for the number of BookingUpdates to be created
for (int c = 0; c < count; c++)
{
    bookingupdate[c] = new BookingUpdate(); // create a new instance of BookingUpdate and assign it the array     
    bookingupdate[c].bookingID = (long)rdr["ID"];
    ...

我希望这个对你有用!



Answer 2:

bookingupdate = new BookingUpdate[c];
bookingupdate[c].bookingID = (long)rdr["ID"]; 

要创建长度的数组c ,这意味着它具有索引0 to (c-1) -然后你超出边界,试图在位置来存储时c



Answer 3:

恕我直言,我将简化你的方式来构建阵列使用LINQ:

BookingUpdate[] bookingupdate = dt.AsEnumerable()
    .Select(r => new BookingUpdate{
        bookingID = r.Field<long>("ID"),
        fullUserName = r.Field<string>("VERANSTALTER"),
        newStart = r.Field<DateTime>("Von"),
        newEnd = r.Field<DateTime>("Bis"), // here was another bug in your originalcode
        newSubject = r.Field<string>("THEMA"),
        newlocation = r.Field<string>("BEZEICHNUNG"),
        deleted = r.Field<string>("STORNO") != null
    })
    .ToArray();

在这条路上,你不会有界限以外的阵列问题。



Answer 4:

您访问的n个元素的数组这是超出范围的第n个元素,你需要访问N - 1元。

bookingupdate = new BookingUpdate[c];   // You create an array of 5 elements for example 
bookingupdate[c].bookingID = (long)rdr["ID"]; // Here you access the 5th elements but there are only 4


Answer 5:

该问题与阵列的尺寸相关;

 for (int c = 0; c < count; c++)
 {
    bookingupdate = new BookingUpdate[c];
    bookingupdate[c].bookingID = (long)rdr["ID"];

在前面的代码,要创建的阵列( bookingupdate在第一大小0); 那么你要插入一个项目。 即使你以某种方式设法跳过第一个,它会再次失败。 只要更新这些线以下;

bookingupdate = new BookingUpdate[count];
for (int c = 0; c < count; c++)
     {        
        bookingupdate[c].bookingID = (long)rdr["ID"];


Answer 6:

            for (int c = 0; c < count; c++)
            {
                bookingupdate = new BookingUpdate[c];

错误是在for循环,其中c为0的这个第一次迭代。 也就是说,你要创建长度为零的数组。 bookingupdate =新BookingUpdate [0];



Answer 7:

你已经初始化数组而不是类本身调用它。 此外,您的初始化是错误的

count = dt.Rows.Count;
bookingupdate = new BookingUpdate[count];
for (int c = 0; c < count; c++)
{
    bu = new BookingUpdate();
    bu.bookingID = (long)rdr["ID"]; // <---- Error is here
    bu.fullUserName = rdr["VERANSTALTER"].ToString();
    bu.newStart = (DateTime)rdr["VON"];
    bu.newStart = (DateTime)rdr["BIS"];
    bu.newSubject = rdr["THEMA"].ToString();
    bu.newlocation = rdr["BEZEICHNUNG"].ToString();
    if (rdr["STORNO"].ToString() != null)
    {
        bu.deleted = true;
    }
    else
    {
        bu.deleted = false;
    }
    bookingupdate[c] = bu;
}


Answer 8:

阵列具有从零开始的索引

当您创建bookingupdate = new BookingUpdate[c]; 你最后的指数将c-1

您不能访问BookingUpdate[c]因为不存在

比方说, c = 4 ,这意味着我们具有4个元件其中它们definedan阵列;

BookingUpdate[0]
BookingUpdate[1]
BookingUpdate[2]
BookingUpdate[3]

BookingUpdate[c]将等于BookingUpdate[4]其中不存在这样的索引。

MSDN页面;

阵列零索引:与n个元素的数组是从0索引到N-1。



Answer 9:

使用此代码。

public BookingUpdate[] getBookingUpdates(string token)
{
String command = "SELECT b.ID,b.VERANSTALTER, rr.VON ,rr.BIS, b.THEMA, b.STORNO, ra.BEZEICHNUNG from BUCHUNG b JOIN RESERVIERUNGRAUM rr on rr.BUCHUNG_ID = b.ID JOIN RAUM ra on ra.ID = rr.RAUM_ID WHERE b.UPDATE_DATE BETWEEN DATEADD (DAY , -20 , getdate()) AND getdate() AND b.BOOKVERNR = 0";
BookingUpdate[] bookingupdate;
SqlConnection connection = new SqlConnection(GetConnectionString());

connection.Open();
try
{
    SqlCommand cmd = new SqlCommand(command, connection);
    SqlDataReader rdr = null;
    int count = 0;
    rdr = cmd.ExecuteReader();


        while (rdr.Read())
        {
            DataTable dt = new DataTable();
            dt.Load(rdr);
            count = dt.Rows.Count;
            bookingupdate = new BookingUpdate[count];
            for (int c = 0; c < count; c++)
            {
                bookingupdate[c].bookingID = (long)rdr["ID"]; // <---- Error is here
                bookingupdate[c].fullUserName = rdr["VERANSTALTER"].ToString();
                bookingupdate[c].newStart = (DateTime)rdr["VON"];
                bookingupdate[c].newStart = (DateTime)rdr["BIS"];
                bookingupdate[c].newSubject = rdr["THEMA"].ToString();
                bookingupdate[c].newlocation = rdr["BEZEICHNUNG"].ToString();
                if (rdr["STORNO"].ToString() != null)
                {
                    bookingupdate[c].deleted = true;
                }
                else
                {
                    bookingupdate[c].deleted = false;
                }

            }

        }

}

catch (Exception ex)
{
    log.Error(ex.Message + "\n\rStackTrace:\n\r" + ex.StackTrace);
}
finally
{
    connection.Close();
}
return bookingupdate;

}



文章来源: indexing Array out of Range Exception