使用while循环的数据绑定VB.NET模型

时间:2016-10-10 14:09:56

标签: vb.net

NET 尝试使用while循环将数据绑定到Stored Procs的模型中;请通过以下代码建议如何工作;使用Telerik Converter转换。

 Try
            Using reader As SqlDataReader = cmd.ExecuteReader()
                While reader.Read()
                login.Add(New LoginDetails() With { _
                    Name = reader("EmployessFullName").ToString(), _
                    Win_UserName = reader("win_username").ToString() _
                })
                End While
            End Using
            con.Close()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

我的实际C#代码如下:

 try
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        login.Add(new LoginDetails()
                        {
                            Name = reader["EmployessFullName"].ToString(),
                            Win_UserName = reader["win_username"].ToString()
                        });
                    }
                }
                con.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

1 个答案:

答案 0 :(得分:0)

根据您的评论,您应该更改代码以使语法与Telerik控件中的操作相匹配(您忘记使用With Keyword):

login.Add(new LoginDetails()
With
{
    Name = reader["EmployessFullName"].ToString(),
    Win_UserName = reader["win_username"].ToString()
});

或者通过将LoginDetails实例化为自己的变量:

LoginDetails loginDetails = new LoginDetails();

loginDetails.Name = reader["EmployessFullName"].ToString()
loginDetails.Win_UserName = reader["win_username"].ToString()

login.Add(loginDetails);