将CheckBox项目保存到数据库访问

问题描述 投票:0回答:2

我有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

结束班

vb.net ado.net
2个回答
0
投票

检查此代码。这是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()

希望这对你有所帮助。


0
投票

对不起,它只保存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
© www.soinside.com 2019 - 2024. All rights reserved.