我有以下功能:
private static DataTable GetDataFromDatabase(DateTime myDate)
{
DataTable table = new DataTable();
table.Columns.AddRange(new DataColumn[]
{
new DataColumn("Event"), new DataColumn("Room"),
new DataColumn("From"), new DataColumn("To"), new DataColumn("Persons"),
new DataColumn("Equipment"), new DataColumn("Catering")
});
string strSQL = "SELECT distinct ID FROM VIEW_RAUMBUCHUNG_DISPO " +
"WHERE BOOKSTATUS >= 1 AND convert(char, von,104) = '" +
BookITVbSQL.to_104(myDate) + "'";
SqlDataReader objRS;
objRS = SQLrunReaderDP(strSQL);
while (objRS.Read())
{
using (SqlConnection con = new SqlConnection(GetConnectionString()))
{
using (SqlCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = "SELECT distinct EVENT, ROOM, CONVERT(char(5), from, 108) " +
"FROM, CONVERT(char(5), to, 108) TO, PERSONS FROM VIEW_RAUMBUCHUNG_DISPO " +
"WHERE ID = '" + objRS["ID"] + "'; " +
"SELECT EQUIPMENTNAME FROM EQUIPMENT WHERE BUCHUNG_ID = '" + objRS["ID"] +
"' and STATUS = '2'; " +
"SELECT CATERINGNAME FROM CATERING WHERE BUCHUNG_ID = '" + objRS["ID"] +
"' and STATUS = '1';";
using (SqlDataReader rdr = cmd.ExecuteReader())
{
do
{
while (rdr.Read())
{
table.Rows.Add(
rdr["EVENT"],
rdr["ROOM"],
rdr["FROM"],
rdr["TO"],
rdr["PERSONS"] );
}
}
while (rdr.NextResult());
rdr.Close();
}
con.Close();
}
}
}
return table;
}
这适用于第一个语句。我可以从五列中获取所有数据。但是如何将其他两个查询中的列添加到同一行?
如果我尝试:
while (rdr.Read())
{
table.Rows.Add(
rdr["EVENT"],
rdr["ROOM"],
rdr["FROM"],
rdr["TO"],
rdr["PERSONS"],
rdr["EQUIPMENTNAME"]);
}
所以我得到一个 IndexOutOfRange - 异常。有人可以帮我吗?