vb.net 将复选框项保存到数据库访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22859774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:18:17  来源:igfitidea点击:

Saving CheckBox Items to Database Access

vb.netado.net

提问by user3287723

i have vb.net application form. It contains ID, Age,Name as Textbox and TC as Checkbox. I have following code to these items, but checkbox items is saved automatically whether checked or not. So what to do?

我有 vb.net 申请表。它包含 ID、年龄、名称作为文本框和 TC 作为复选框。我对这些项目有以下代码,但无论是否选中,复选框项目都会自动保存。那么该怎么办?

[Imports System.Data.OleDb
Imports System.Data
Public Class Form1
    Dim con As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim con_str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        con.ConnectionString = con_str
        con.Open()
        'MsgBox(con.State)

        Dim myReader As OleDbDataReader
        cmd = New OleDbCommand("select * from Table1", con)
        myReader = cmd.ExecuteReader
        While myReader.Read()

        End While

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        con.Close()

    End Try
End Sub

Private Sub AddTable1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddTable1.Click
    Try
        con.ConnectionString = con_str
        con.Open()
        'MsgBox(con.State)

        cmd = New OleDbCommand("insert into Table1(ID,Age,Name,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Nametxt.Text & "','" & TCtxt.Text & "')", con)
        cmd.ExecuteNonQuery()
        MsgBox("Added Successfuly")

        Dim myReader As OleDbDataReader
        cmd = New OleDbCommand("select * from Table1", con)
        myReader = cmd.ExecuteReader
        Agetxt.Clear()

        While myReader.Read()

        End While

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        con.Close()

    End Try

End Sub

End Class

结束班

回答by Sarjit Delivala

Check this code. This is for 3 checkboxes. You can extend it for 10 checkboxes. Have a look here:

检查此代码。这是用于 3 个复选框。您可以将其扩展为 10 个复选框。看看这里:

    Dim hbStr As String
    Dim bgStr As String
    Dim tcStr As String

    If hb.Checked Then
        hbStr = hb.Text
    Else
        hbStr = ""
    End If

    If bg.Checked Then
        bgStr = bg.Text
    Else
        bgStr = ""
    End If

    If tc.Checked Then
        tcStr = tc.Text
    Else
        tcStr = ""
    End If

    Dim cmd As New OleDbCommand
    Dim conn As New OleDbConnection("Connection String")
    conn.Open()
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "insert into Table1(ID,Age,Name,TC,HB.BG) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Nametxt.Text & "','" & tcStr & "','" & hbStr & "','" & bgStr & "')"
    cmd.ExecuteNonQuery()

Hope this helps you.

希望这对你有帮助。

回答by user3287723

Sorry it save only TC not Hb.

抱歉,它只保存 TC 而不是 Hb。

If Hbtxt.Checked Then
   cmd = New OleDbCommand("insert into Table1(ID,Age,Hb) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Hbtxt.Text & "')", con)
Else
   cmd = New OleDbCommand("insert into Table1(ID,Age,Hb,) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','')", con)
End If

If TCtxt.Checked Then
  cmd = New OleDbCommand("insert into Table1(ID,Age,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & TCtxt.Text & "')", con)
Else
  cmd = New OleDbCommand("insert into Table1(ID,Age,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','')", con)
End If