Retrieving a bit column from sql database in VB NET

喜你入骨 提交于 2019-12-31 02:49:29

问题


I have encountered a problem where in my database I have a column that is a bit either 0 or 1 specifying if a user is an admin or is account suspended or not. And in my VB code I am trying to retrieve that bit. Example code:

    Dim dtRequests As DataTable
    dtRequests = New DataTable("Users")
    dtRequests.Columns.Add("SESLoginID", System.Type.GetType("System.Int32"))
    dtRequests.Columns.Add("fullname", System.Type.GetType("System.String"))
    dtRequests.Columns.Add("SESSuspended", System.Type.GetType("System.Byte"))
    dtRequests.Columns.Add("SESAdmin", System.Type.GetType("System.Byte"))
    dtRequests.Columns.Add("OfficeName", System.Type.GetType("System.String"))

    cmdoledb = New SqlCommand("SELECT SESLoginID, SESFirstName + ' ' + SESLastName As fullname , SESSuspended, SESAdmin, OfficeID from SESLogin where (SESEmail Like '%" & keyword & "%' or SESFirstName + ' ' + SESLastName like '%" & keyword & "%' or SESLastName like '%" & keyword & "%') order by SESFirstName;", objConnect)
    cmdoledb.Parameters.AddWithValue("@pid", pid)
    objConnect.Open()

    Dim rdr As SqlDataReader
    Dim myItem As ListItem = New ListItem()
    rdr = cmdoledb.ExecuteReader()

    While rdr.Read()
        Dim newrow As DataRow
        newrow = dtRequests.NewRow
        newrow.Item("SESLoginID") = rdr.GetInt32(0)
        newrow.Item("fullname") = rdr.GetString(1)
        newrow.Item("SESSuspended") = rdr.GetByte(2)  // Specified cast is not valid.
        newrow.Item("SESAdmin") = rdr.GetByte(3) // Specified cast is not valid.

        Dim officeid As Integer = rdr.GetInt32(4)
        Dim officename As String = ""

        cmdldb = New SqlCommand("SELECT OfficeName from Office where OfficeID = @offid", objConnected)
        cmdldb.Parameters.AddWithValue("@offid", officeid)
        objConnected.Open()

        officename = cmdldb.ExecuteScalar()
        newrow.Item("OfficeName") = officeid
        objConnected.Close()
        dtRequests.Rows.Add(newrow)
    End While

I tried getting rdr.GetByte(3), this tells me cast not valid, but there is no function of which will say GetBit, and if such exists I could not find it. Thus Im hoping for a quick response.


回答1:


Try rdr.GetBoolean(3) to get Bit values. A bit in SQL Server is the same as a boolean in VB, except it uses 1 and 0 instead of true and false.




回答2:


Use GetBoolean

Although there is no "boolean" SQL data type, bit maps to .net boolean




回答3:


You want

  newrow.Item("SESSuspended") = rdr.GetBoolean(2)
  newrow.Item("SESAdmin") = rdr.GetBoolean(3)

You should also change these lines

  dtRequests.Columns.Add("SESSuspended", System.Type.GetType("System.Byte"))
  dtRequests.Columns.Add("SESAdmin", System.Type.GetType("System.Byte"))

to

dtRequests.Columns.Add("SESSuspended", System.Type.GetType("System.Boolean"))
dtRequests.Columns.Add("SESAdmin", System.Type.GetType("System.Boolean"))



回答4:


You can specify the name of the Data Item rather than the index. Such as:

newrow.Item("SESSuspended") = rdr("SESSuspended")


来源:https://stackoverflow.com/questions/6827222/retrieving-a-bit-column-from-sql-database-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!