0

我有以下功能:

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 - 异常。有人可以帮我吗?

4

1 回答 1

0

SqlDataReader 类:

提供一种从 SQL Server 数据库中读取只进的行流的方法。

您可以使用这个类来创建一个带有 2 个连接的语句。它将返回 7 列

"SELECT distinct EVENT, ROOM, CONVERT(char(5), from, 108) FROM, CONVERT(char(5), to, 108) TO, PERSONS, EQUIPMENTNAME, CATERINGNAME FROM VIEW_RAUMBUCHUNG_DISPO T1"

+ "INNER JOIN (SELECT EQUIPMENTNAME FROM EQUIPMENT) T2 ON T1.ID = T2.BUCHUNG_ID"

+ "INNER JOIN (SELECT CATERINGNAME FROM CATERING) T3 ON T1.ID = T3.BUCHUNG_ID" +

+ "WHERE T1.ID = '" + objRS["ID"] + "' AND T2.STATUS = '2' AND T3.STATUS = '1' "

更新

使用NextResult方法:

读取批处理 Transact-SQL 语句的结果时,将数据读取器推进到下一个结果

如果您需要访问整个记录集,而不是一次访问行,您可以使用 SqlDataAdapter ( SqlDataAdapter vs SqlDataReader )。

于 2013-06-14T13:29:14.950 回答