Retrieving a bit column from sql database in VB NET

前端 未结 4 1815
闹比i
闹比i 2021-01-22 22:03

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

相关标签:
4条回答
  • 2021-01-22 22:27

    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.

    0 讨论(0)
  • 2021-01-22 22:37

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

    newrow.Item("SESSuspended") = rdr("SESSuspended")
    
    0 讨论(0)
  • 2021-01-22 22:39

    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"))
    
    0 讨论(0)
  • 2021-01-22 22:48

    Use GetBoolean

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

    0 讨论(0)
提交回复
热议问题