如何在 vb.net 中将单选按钮 true 或 false 插入到数据库 ms.access 中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21555790/
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 16:39:28  来源:igfitidea点击:

How to insert radio button true or false to database ms.access in vb.net?

vb.net

提问by Ujang Hardman

i want if i checked radiobutton1 then field status(data type: yes/no, format: true/false) in database ms.access is true(checked), if i checked radiobutton2 then field status(data type: yes/no, format: true/false) in database ms.access is false(unchecked).

我想如果我检查了 radiobutton1 然后字段状态(数据类型:是/否,格式:真/假)在数据库 ms.access 是真(选中),如果我检查 radiobutton2 然后字段状态(数据类型:是/否,格式: true/false) 在数据库 ms.access 中为 false(未选中)。

my script is:

我的脚本是:

dim s as boolean
if radiobutton1.checked = true then
s = true
elseif radiobutton2.checked = true then
s = false
end if

                cmd = New OleDbCommand("Insert into table_petugas (name,status) Values ('" & txtname.Text & "','" & status & "' )", conn)
                cmd.ExecuteNonQuery()
                MsgBox("Data Saved!", MsgBoxStyle.Information, "PERHATIAN!")

but the script is error, saying: "data type mismatch in criteria expression" help me, please.. sorry for my bad language..

但脚本是错误的,说:“条件表达式中的数据类型不匹配”请帮助我.. 抱歉我的语言不好..

回答by Steve

Use a parameterized query

使用参数化查询

cmd = New OleDbCommand("Insert into table_petugas (name,status) Values (?,?)", conn)
cmd.Parameters.AddWithValue("@p1", txtname.Text)
cmd.Parameters.AddWithValue("@p2", s) 
cmd.ExecuteNonQuery()

This approach pass the work to correctly define the values required by your database fields to the framework code that create a boolean parameter and update your field.

这种方法将正确定义数据库字段所需的值的工作传递给创建布尔参数并更新字段的框架代码。

Of course this will avoid also any possible Sql Injection scenario

当然,这也将避免任何可能的 Sql 注入场景

回答by TutiBueno

Maybe it is your "status" variable which is not present on the code?

也许这是代码中不存在的“状态”变量?

dim s as boolean
if radiobutton1.checked = true then
s = true
elseif radiobutton2.checked = true then
s = false
end if

cmd = New OleDbCommand("Insert into table_petugas (name,status) Values ('" & txtname.Text & "','" & s & "' )", conn)
cmd.ExecuteNonQuery()
MsgBox("Data Saved!", MsgBoxStyle.Information, "PERHATIAN!")