Указанное приведение недействительно для RowNumber

1

У меня есть этот код

  using (SqlConnection myConnection = new SqlConnection(AppConfiguration.ConnectionStringName))
        {
            Helpers helpers = new Helpers();
            myConnection.ConnectionString = helpers.GetConnectionString(myUser.ServerName, myConnection.ConnectionString);
            using (SqlCommand myCommand = new SqlCommand("Select ROW_NUMBER() OVER(ORDER BY PersonelNo DESC) AS Row,* from View_Insurance", myConnection))
            {
                myConnection.Open();
                using (SqlDataReader myReader = myCommand.ExecuteReader())
                {                       
                    while (myReader.Read())
                    {
                        InsuranceEntities insuranceEntity = new InsuranceEntities();
                        insuranceEntity.Row = myReader.GetInt32(myReader.GetOrdinal("Row"));
                        insuranceEntity.Id = myReader.GetInt32(myReader.GetOrdinal("Id"));
                        insuranceEntities.Add(insuranceEntity);
                    }
                    myReader.Close();
                }
            }
            myConnection.Close();
        }

и Сущность:

  class InsuranceEntities
{
    public int Row { get; set; }
    public string FName{ get; set; }


    public InsuranceEntities()
    {
    }
    public InsuranceEntities(int row,string  fName)           
    {
        this.Row = row;
        this.FName= fName;           
    }  
}

но когда он достигает линии

   insuranceEntity.Row = myReader.GetInt32(myReader.GetOrdinal("Row"));

Я получаю следующую ошибку

   Specified cast is not valid

Я определил строку как целое, и я получаю это как Int32, так почему я получаю это сообщение об ошибке?

Спасибо

Теги:
sql-server
visual-studio-2010

1 ответ

2
Лучший ответ

Вы должны бросить его надолго

Попробуйте изменить код на

//I guess , or similar 
insuranceEntity.Row = myReader.GetInt64(myReader.GetOrdinal("Row"));

видеть

http://social.msdn.microsoft.com/forums/sqlserver/en-US/e3ced4f6-2514-4a1a-87d3-d19e4ccd6d63/rownumber-data-type

http://msdn.microsoft.com/en-us/library/cc716729.aspx

Ещё вопросы

Сообщество Overcoder
Наверх
Меню