1

我尝试将 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;
}

我错过了什么?

4

9 回答 9

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

您正在创建一个长度数组c,这意味着它具有索引0 to (c-1)- 然后您在尝试存储在 Position 时超出范围c

于 2013-02-13T09:02:42.310 回答
1

恕我直言,我会简化您使用 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();

这样,您将不会遇到超出范围的数组问题。

于 2013-02-13T09:07:07.423 回答
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"];
    ...

我希望这个对你有用!

于 2013-02-13T09:14:29.260 回答
0

您访问超出范围的 n-Elements 数组的第 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
于 2013-02-13T09:03:21.030 回答
0

数组具有从零开始的索引

当您创建bookingupdate = new BookingUpdate[c];最后一个索引时将c-1.

您无法访问BookingUpdate[c],因为它不存在

比方说c = 4,这意味着我们定义了一个包含 4 个元素的数组;

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

BookingUpdate[c]将等于BookingUpdate[4]没有这样的索引。

MSDN页面;

数组索引为零:具有n 个元素的数组索引从0n-1

于 2013-02-13T09:04:33.253 回答
0

问题与数组的大小有关;

 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"];
于 2013-02-13T09:05:43.947 回答
0
            for (int c = 0; c < count; c++)
            {
                bookingupdate = new BookingUpdate[c];

错误是这个 for 循环的第一次迭代,其中 c 为零。即,您正在尝试创建一个长度为零的数组。bookingupdate = 新的 BookingUpdate[0];

于 2013-02-13T09:06:10.410 回答
0

在调用它之前,您已经初始化了一个数组而不是类本身。你的初始化也是错误的

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;
}
于 2013-02-13T09:07:07.790 回答
0

使用此代码

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;

}

于 2013-02-13T09:10:34.400 回答