VB.net - 如果是1那么"是"如果为0那么" no"

时间:2014-09-18 19:38:16

标签: sql vb.net

我正在尝试制作数据库软件。问题是我似乎无法找到如何使这项工作,我对这一切都是新手,并且我自己想了很多,但我似乎无法做这个简单的任务?

我有一个listView,显示来自SQl的数据一切都很好除了我需要dropdaown框说是或否但是导入SQL数据库1或0并且在我的列表视图中我需要它来显示一个是或否是而不是1或0?在此先感谢

代码:

Imports System.Data.SqlClient
Imports System.Data

Public Class cmListAll
 Dim cn As New SqlConnection
 Dim cmd As SqlCommand
 Dim dr As SqlDataReader

 Private Sub frmReg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    With Me.cboActive
        .Items.Add("Yes")
        .Items.Add("No")
        .SelectedIndex = 0
    End With
    Call connectMeToSQLServer("Data Source=Database;Initial Catalog=db_XXX;Integrated Security=False;Uid=sa; Pwd=PASS;")
    Call showList()
 End Sub

 Private Sub cboActive_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    e.Handled = True
 End Sub

 Private Sub cboACTIVE_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Me.txtCredentials.Focus()
 End Sub

 Sub connectMeToSQLServer(ByVal cnString As String)
    Try
        With cn
            If .State = ConnectionState.Open Then .Close()
            .ConnectionString = cnString
            .Open()
        End With
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try
 End Sub

Function INC() As Boolean
    For Each t In Me.GroupBox2.Controls
        If TypeOf t Is TextBox Or TypeOf t Is ComboBox Then

        End If
        If t.Text = "" Then
            INC = True
        End If
    Next
End Function

Sub showList()
    cmd = New SqlCommand
    cmd.Connection = cn
    cmd.CommandText = "Select * from [Case Managers]"
    dr = cmd.ExecuteReader

    Me.ListView1.Items.Clear()
    While dr.Read
        With Me.ListView1
            .Items.Add(dr(0))
            With .Items(.Items.Count - 1).SubItems
                .Add(dr(1))
                .Add(dr(2))
                .Add(dr(3))
                .Add(dr(4))
            End With
        End With
    End While
    dr.Close()
End Sub

Sub clearMe()
    For Each t In Me.GroupBox2.Controls
        If TypeOf t Is TextBox Then
            If t.Text <> "" Then
                t.text = ""
            End If
            Me.cmdNew.Enabled = True
            Me.cmdSave.Text = "&Save"
            Me.cmdSave.Enabled = False
            Me.cmdDelete.Enabled = False
            Me.cboActive.SelectedIndex = 0
        End If
    Next
End Sub

Private Sub cmdNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNew.Click
    For Each t In Me.GroupBox2.Controls
        If TypeOf t Is TextBox Then
            If t.Text <> "" Then
                t.text = ""
            End If
        End If
    Next
    Me.cmdNew.Enabled = False
    Me.cmdSave.Tag = "SAVE"
    Me.cmdSave.Text = "&Save"
    Me.cmdSave.Enabled = True
    Me.GroupBox2.Enabled = True
    Me.txtfirstname.Focus()
End Sub

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    Select Case Me.cmdSave.Tag
        Case "SAVE"
            If INC() = True Then
                MsgBox("Please Complete All Fields!", MsgBoxStyle.Exclamation, "")
                Exit Sub
            Else
                cmd = New SqlCommand
                cmd.CommandText = "Insert Into [Case Managers](Firstname,Lastname,Credentials,Active) Values('" & Me.txtfirstname.Text & "', '" & Me.txtlastname.Text & "', '" & Me.txtCredentials.Text & "', '" & Me.cboActive.Text & "' )"
                cmd.Connection = cn
                cmd.ExecuteNonQuery()
                MsgBox("Successfully Save!", MsgBoxStyle.Information, "")
            End If
        Case Else
            cmd = New SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "Update [Case Managers] Set firstname='" & Me.txtfirstname.Text & "', lastname='" & Me.txtlastname.Text & "', credentials='" & Me.txtCredentials.Text & "', active='" & Me.cboActive.Text & "' Where CaseMangerID = " & Me.ListView1.SelectedItems(0).Text & ""
            cmd.ExecuteNonQuery()
            MsgBox("Successfully Updated!", MsgBoxStyle.Information, "")
    End Select
    clearMe()
    showList()
End Sub

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    cmd = New SqlCommand
    cmd.Connection = cn
    cmd.CommandText = "Select * from [Case Managers] Where CaseMangerID = " & Me.ListView1.SelectedItems(0).Text & " "
    dr = cmd.ExecuteReader
    dr.Read()
    Me.txtfirstname.Text = dr(1)
    Me.txtlastname.Text = dr(2)
    Me.txtCredentials.Text = dr(3)
    Me.cboActive.Text = dr(4)
    dr.Close()
    Me.GroupBox2.Enabled = True
    Me.cmdSave.Enabled = True
    Me.cmdSave.Tag = "UPDATE"
    Me.cmdSave.Text = "&Update"
    Me.cmdDelete.Enabled = True
End Sub

Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
    If MsgBox("Delete This Record?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
        cmd = New SqlCommand
        cmd.Connection = cn
        cmd.CommandText = "Delete from [Case Managers] Where CaseMangerID =" & Me.ListView1.SelectedItems(0).Text & " "
        cmd.ExecuteNonQuery()
        MsgBox("Successfully Deleted!", MsgBoxStyle.Information, "")
        Me.cmdDelete.Enabled = False
        Me.cmdSave.Enabled = False
        Call clearMe()
        Call showList()
    Else
        Exit Sub
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

我假设你正在看这一行:

Me.cboActive.Text = dr(4)

?这是唯一一个看起来像我可能会显示是/否字段的东西。您可以在其中放置一个简单的If / Else块,但由于此代码的结构存在其他几个重大缺陷,我认为为您重写整个方法会很有用:

Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
    'Would be better to specify column names here
    Dim sql As String = "Select * from [Case Managers] Where CaseMangerID = @CaseManager"

    'Best practices in most cases call for creating a new connection object for each query
    Using cn As New SqlConnection("connection string here"), _
          cmd As New SqlCommand(sql, cn)

        'This is how to do string substitution in sql to protect against sql injection
        cmd.Parameters.Add("@CaseManager", SqlDbType.Int).Value = CInt(Me.ListView1.SelectedItems(0).Text)

        Using dr As SqlDataReader = cmd.ExecuteReader()
            dr.Read()
            Me.txtfirstname.Text = dr(1)
            Me.txtlastname.Text = dr(2)
            Me.txtCredentials.Text = dr(3)
            Me.cboActive.Text = If(dr(4)=0,"No","Yes")
            dr.Close()
        End Using
    End Using 'No need to close the connection. The Using block takes care of it
    'The old code would have the left the connection open if an exception was thrown,'
    ' which could eventually lock you out of the database

    Me.GroupBox2.Enabled = True
    Me.cmdSave.Enabled = True
    Me.cmdSave.Tag = "UPDATE"
    Me.cmdSave.Text = "&Update"
    Me.cmdDelete.Enabled = True
End Sub

稍后您需要执行类似的操作以将其反转以进行更新/插入。

答案 1 :(得分:0)

您可以使用DisplayMember / ValueMember对:

Dim dict As New Dictionary(Of Integer, String)
dict.Add(0, "No")
dict.Add(1, "Yes")

With Me.cboActive
    .DisplayMember = "Value"
    .ValueMember = "Key"
    .DataSource = dict.ToList()
    .SelectedIndex = 0
End With

词典在这里可能不是最好的选择,但可以作为概念证明。

然后只需使用cboActive.SelectedValue并将其传递给SQL查询。