简体   繁体   English

如何从 SqlDataReader 获取 int32 和字符串。 获取错误 System.IndexOutOfRangeException: 'StudentID'

[英]How to get int32 and strings from SqlDataReader. Getting error System.IndexOutOfRangeException: 'StudentID'

I am working on a personal project that is using a Microsoft SQL DB and a web layer for Rest API calls.我正在开发一个使用 Microsoft SQL DB 和用于 Rest API 调用的 Web 层的个人项目。 I am trying to get int32s and strings back from the database using a SqlDataReader.我正在尝试使用 SqlDataReader 从数据库中获取 int32s 和字符串。 When I read in Strings there is no error, however when I attempt to read integers I get the following当我在字符串中读取时没有错误,但是当我尝试读取整数时,我得到以下信息

`Exception: System.IndexOutOfRangeException: 'StudentID' `异常:System.IndexOutOfRangeException:'StudentID'

public StudentAccount FindStudentByID(int id)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        String sql = "SELECT FirstName, LastName,StudentBiography FROM [PersonTest].[dbo].[StudentAccounts] WHERE StudentID = @p1";
        StudentAccount matchingStudent = new StudentAccount();

        using (SqlCommand cmd = new SqlCommand(sql, connection))
        {
            cmd.Parameters.Add("@p1", SqlDbType.Int).Value = id;
            cmd.CommandType = CommandType.Text;

            SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    matchingStudent.StudentID = reader.GetInt32(reader.GetOrdinal("StudentID"));

                    matchingStudent.FirstName = reader["FirstName"].ToString();
                    matchingStudent.LastName = reader["LastName"].ToString();
                    matchingStudent.Biography = reader["StudentBiography"].ToString();

                    matchingStudent.CollegeID = reader.GetInt32(reader.GetOrdinal("COLLTB_CollegeID"));
                }

            reader.Close();

            return matchingStudent;
        }
    }
}

The StudentAccounts table is as following: StudentAccounts 表如下:

StudentAccounts (StudentID, FirstName, LastName, StudentBiography, CollTB_CollegeID)

The StudentID is the primary key. StudentID是主键。

I apologize ahead of time if I messsed up the format or anything, this is my first time posting on this forum.如果我弄乱了格式或任何东西,我提前道歉,这是我第一次在这个论坛上发帖。 Thanks!谢谢!

You're not returning StudentID or COLLTB_CollegeID as columns in your select statement...您没有将StudentIDCOLLTB_CollegeID作为列返回到您的select语句中...

String sql = "SELECT FirstName, LastName,StudentBiography FROM [PersonTest].[dbo].[StudentAccounts] WHERE StudentID = @p1";

Should be...应该...

String sql = "SELECT StudentID, FirstName, LastName, StudentBiography, COLLTB_CollegeID FROM [PersonTest].[dbo].[StudentAccounts] WHERE StudentID = @p1";

Although you appear to be setting matchingStudent.CollegeID twice, so it doesn't look like you need StudentID in your return columns anyway!虽然您似乎设置了matchingStudent.CollegeID两次,但看起来您的返回列中并不需要StudentID

You're using:您正在使用:

matchingStudent.CollegeID = reader.GetInt32(reader.GetOrdinal("StudentID"));

This means:这意味着:

var columnNumber = reader.GetOrdinal("StudentID");
matchingStudent.CollegeID = reader.GetInt32(columnNumber);

Now the first call fails, because StudentID is not in your SELECT statement.现在第一次调用失败,因为StudentID不在您的SELECT语句中。 Include the columns you want to read.包括您要阅读的列。 The same goes for COLLTB_CollegeID . COLLTB_CollegeID

So:所以:

var sql = "SELECT FirstName, LastName, StudentBiography, StudentID, COLLTB_CollegeID FROM [PersonTest].[dbo].[StudentAccounts] WHERE StudentID = @p1";

Also, you're assigning the result to the wrong property:此外,您将结果分配给错误的属性:

matchingStudent.StudentID = reader.GetInt32(reader.GetOrdinal("StudentID"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM