1

How to add Yes or no in the database using a check box based on check or uncheck if check then Yes and vice versa.?

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
        Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Taher\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\Database1.accdb;Persist Security Info=False;"
        Me.con = New OleDb.OleDbConnection()
        con.ConnectionString = dbprovider
        con.Open()

        Dim sqlquery As String = "INSERT INTO Company (dev)" + "VALUES ('" Yes "')
        Dim sqlcommand As New OleDb.OleDbCommand(sqlquery)
    End If
End Sub
4

3 回答 3

2

使用这种方式

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Taher\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\Database1.accdb;Persist Security Info=False;"
    Me.con = New OleDb.OleDbConnection()
    con.ConnectionString = dbprovider
    con.Open()
    Dim sqlquery As String

    sqlquery = "Insert into Company (dev) values ('" + IIf(CheckBox1.Checked, "yes", "no") + "')"

    Dim sqlcommand As New OleDb.OleDbCommand(sqlquery)
    sqlcommand.executeNonQuery()

结束子

于 2013-02-22T12:09:29.203 回答
1

使用 sql 类型BIT来表示 False (0) 或 True (1)。您可以使用 TRUE 和 FALSE 来插入数据。

于 2013-02-22T11:55:45.400 回答
1

复选框本质上是布尔输入。您可以使用相当多的方法将它们的值存储在数据库中,这些是最常见的: - 布尔字段(真/假) - 数字字段(1 代表真,0 代表假)

使用布尔关键字 TRUE 和 FALSE

TRUE 等于是 FALSE 等于否

UPDATE tablename SET field1 = TRUE, field2 = FALSE;

INSERT INTO tablename SET field1 = TRUE, field2 = FALSE ;

希望这可以帮助

于 2013-02-22T11:55:54.210 回答