繁体   English   中英

布尔中的数据类型不匹配错误

[英]Data type mismatch error in boolean

我需要对用户的VotingStatus(Boolean)进行身份验证,一旦在数据库中检查了他的投票状态,他就不能再次投票。

下面的代码显示数据类型不匹配错误。 这有什么问题?

MessageBox.Show("Welcome!");
OleDbCommand comd1 = new OleDbCommand();
comd1.Connection = connection;
comd1.CommandText = "SELECT VoterID FROM tbl_voter where Uname='" + txt_user.Text + "' and Pword='" + txt_pass.Text + "'";
voterid = Convert.ToString(comd1.ExecuteScalar());
Program.VoterID = voterid;

string comd21 = "Select VotingStatus from tbl_voter where VoterID='" + voterid + "'";
OleDbCommand comd2 = new OleDbCommand(comd21, connection);
var vstatus = (String)comd2.ExecuteScalar();
if (vstatus == "true")
{
      MessageBox.Show("You cannot vote again!");
}

您应该更改以下几行

var vstatus = (String)comd2.ExecuteScalar();
if (vstatus == "true")

如下:

var vstatus = comd2.ExecuteScalar();
if (vstatus !=null && Convert.ToBoolean(vstatus))
{
    MessageBox.Show("You cannot vote again!");
}

尽管与您提出的要求无关,但应该指出,在创建SQL语句时必须避免使用字符串连接。 不这样做,您就可以接受SQL注入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM