查询抛出“IndexOutOfRange”

时间:2012-11-25 15:50:31

标签: c# sql sql-server tsql

我觉得这很奇怪,我有这个代码:

string getBatchesQuery = "SELECT DISTINCT CONVERT(VARCHAR(8), time, 4) AS Batch FROM Calls ORDER BY Batch";
SqlCommand getBatchesQueryCom = new SqlCommand(getBatchesQuery, connection);

SqlDataReader getBatchesQueryReader = myCom.ExecuteReader();

List<string> allBatches = new List<string>();

while (getBatchesQueryReader.Read())
{

    allBatches.Add(getBatchesQueryReader["Batch"].ToString());

}
getBatchesQueryReader.Close();

现在怎么可能Batch不存在?也许是因为我这样命名?

我该怎么办?

1 个答案:

答案 0 :(得分:2)

该列不存在,因为您运行的命令错误。将myCom更改为getBatchesQueryCom

SqlDataReader getBatchesQueryReader = getBatchesQueryCom.ExecuteReader();