VS / SQLServer无法使用组合框作为数据表更新数据库

时间:2016-04-08 15:57:42

标签: vb.net visual-studio-2012

这是我的代码,除了ONE之外它完美无缺。我无法更新:

 Imports System.Data
 Imports System.Data.SqlClient

 Public Class Form1
   Dim con As SqlConnection
   Dim Cmd As SqlCommand
   Dim Query As String

 Sub disk()
    con = New SqlConnection
    con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
    Try
        con.Open()
        Dim READER As SqlDataReader
        Query = "UPDATE AdminESP SET Benefactor='" & CmbBenefactor.Text & "' WHERE ExternalSP='" & CmbExternalSP.Text & "'"
        Cmd = New SqlCommand(Query, con)
        READER = Cmd.ExecuteReader()

        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        con.Close()
    End Try
    con.Close()
End Sub
Sub dis()
    CmbExternalSP.Items.Clear()
    CmbBenefactor.Items.Clear()
    con = New SqlConnection
    con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
    Try
        con.Open()
        Dim READER As SqlDataReader
        Query = "select * from [AJLMNewProjectDatabase].[dbo].[AdminESP]"
        Cmd = New SqlCommand(Query, con)
        READER = Cmd.ExecuteReader()
        While READER.Read
            Dim ESP = READER.GetString(1)
            Dim Ben = READER.GetString(2)
            CmbExternalSP.Items.Add(ESP)
            CmbBenefactor.Items.Add(Ben)
        End While
        con.Close()
    Catch ex As SqlException
        MessageBox.Show(ex.Message)
    Finally
        con.Dispose()
    End Try
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    con = New SqlConnection
        con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
    Try
        con.Open()
        Dim READER As SqlDataReader
        Query = "select * from AdminESP"
        Cmd = New SqlCommand(Query, con)
        READER = Cmd.ExecuteReader()
        While READER.Read
            Dim ESP = READER.GetString(1)
            Dim Ben = READER.GetString(2)
            CmbExternalSP.Items.Add(ESP)
            CmbBenefactor.Items.Add(Ben)
        End While
        con.Close()
    Catch ex As SqlException
        MessageBox.Show(ex.Message)
    Finally
        con.Dispose()
    End Try
End Sub
Private Sub CmbExternalSP_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbExternalSP.SelectedIndexChanged
    CmbBenefactor.SelectedIndex = CmbExternalSP.SelectedIndex
End Sub

Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
    con = New SqlConnection("Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;")

    con.Open()

    Cmd = con.CreateCommand

    Cmd.CommandText = String.Format("INSERT INTO AdminESP(ExternalSP, Benefactor) " &
                                    "VALUES('{0}', '{1}')",
                                    CmbExternalSP.Text,
                                    CmbBenefactor.Text) ' To Save data to ExternalSP DataTable

    Dim rowsAffected As Integer = Cmd.ExecuteNonQuery()

    If rowsAffected > 0 Then
        MsgBox("Program Added!")
    Else
        MsgBox("Failed to Add")
    End If
    CmbExternalSP.Text = ""
    CmbBenefactor.Text = ""
    con.Close()
End Sub

Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
    CmbExternalSP.DropDownStyle = ComboBoxStyle.DropDown
    CmbBenefactor.DropDownStyle = ComboBoxStyle.DropDown

    CmbExternalSP.Text = ""
    CmbBenefactor.Text = ""
End Sub

Private Sub BtnRemove_Click(sender As Object, e As EventArgs) Handles BtnRemove.Click
    con = New SqlConnection
    con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
    Try
        con.Open()
        Dim READER As SqlDataReader
        Query = "DELETE FROM AdminESP WHERE ExternalSP='" + CmbExternalSP.Text + "'"
        Cmd = New SqlCommand(Query, con)
        READER = Cmd.ExecuteReader()
        If MessageBox.Show("You are about to remove a Program, Please confirm your Action.", "DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.OK Then
            MsgBox("Program Removed")
        End If
        CmbExternalSP.Text = ""
        CmbBenefactor.Text = ""
        CmbExternalSP.DropDownStyle = ComboBoxStyle.DropDownList
        CmbBenefactor.DropDownStyle = ComboBoxStyle.DropDownList
        Call dis()
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        con.Close()
    End Try
    con.Close()
End Sub
Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click
    con = New SqlConnection
    con.ConnectionString = "Server=Jansen\SQLEXPRESS;Database=AJLMNewProjectDatabase;Trusted_Connection=True;"
    Try
        con.Open()
        Dim READER As SqlDataReader
        Query = "UPDATE AdminESP SET ExternalSP='" & CmbExternalSP.Text & "' WHERE Benefactor='" & CmbBenefactor.Text & "'"
        Cmd = New SqlCommand(Query, con)
        READER = Cmd.ExecuteReader()

        MessageBox.Show("Program Updated.")

        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        con.Close()
    End Try
    con.Close()
End Sub

Private Sub BtnR_Click(sender As Object, e As EventArgs) Handles BtnR.Click
    Call dis()
End Sub

Private Sub CmbBenefactor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbBenefactor.SelectedIndexChanged
    CmbExternalSP.SelectedIndex = CmbBenefactor.SelectedIndex
End Sub
End Class

现在我的问题是,当我点击UPDATE按钮时,只有一个在combobox 1下的列更新;另一个组合框根本没有变化。

0 个答案:

没有答案