索引数组超出范围异常
本文关键字:范围 异常 数组 索引 | 更新日期: 2023-09-27 17:59:10
我尝试将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;
}
我错过了什么?
bookingupdate = new BookingUpdate[c];
bookingupdate[c].bookingID = (long)rdr["ID"];
您正在创建一个长度为c
的数组,这意味着它具有索引0 to (c-1)
,然后当您尝试在位置c
存储时,您就越界了。
您似乎正在为具有的数组创建和分配内存
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"];
...
我希望这能有所帮助!
Imho i将简化您使用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();
这样,您就不会遇到数组越界的问题。
如果访问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
问题与数组的大小有关;
for (int c = 0; c < count; c++)
{
bookingupdate = new BookingUpdate[c];
bookingupdate[c].bookingID = (long)rdr["ID"];
在上一段代码中,您首先要创建一个大小为0的数组(bookingupdate
);那么您正试图插入一个项目。即使你设法跳过了第一个,它也会再次失败。只需将这些行更新为以下内容;
bookingupdate = new BookingUpdate[count];
for (int c = 0; c < count; c++)
{
bookingupdate[c].bookingID = (long)rdr["ID"];
for (int c = 0; c < count; c++)
{
bookingupdate = new BookingUpdate[c];
对于c为零的循环,错误出现在第一次迭代时。即,您正试图创建一个长度为零的数组bookingupdate=新的bookingupdate[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;
}
数组具有基于零的索引。
创建bookingupdate = new BookingUpdate[c];
时,最后一个索引为c-1
。
您无法访问BookingUpdate[c]
,因为它不存在。
比方说c = 4
,这意味着我们定义了一个有4个元素的数组,它们是什么;
BookingUpdate[0]
BookingUpdate[1]
BookingUpdate[2]
BookingUpdate[3]
则CCD_ 9将等于不存在这样的索引的CCD_。
来自MSDN
页面;
数组为零索引:具有n元素的数组从0索引到n-1。
使用此代码
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;
}