如何将布尔值插入到数据库中。

4

我需要将用户数据添加到名为“employees”的数据库表中。它有ID、姓名、姓氏、用户名、密码、电子邮件、地址、管理员权限等选项。

管理员权限是布尔选项(是或否)。

如果我创建了一个包含所有数据的表单,并且我想通过复选框检查用户是否具有管理员权限(选中 - true,未选中 - false),我该怎么做呢?

这是我的语句,因为我不知道如何包括admin_rights。

请帮帮我。

string write = "Insert into Employees (ID, Name, Last_name, Username, Password, E_mail, Address) values('" + this.ID.Text + "','" + this.name.Text + "','" + this.lastName.Text + "','" + this.userName.Text + "','" + this.password.Text + "','" + this.eMail.Text + "','" + this.address.Text + "');";

感谢大家的回复。

12
你的代码极易遭受SQL注入攻击。为了保护自己(和用户),请使用预处理语句。 - p.s.w.g
3
什么数据库?SQL Server没有“bool”类型。不过它有一个位(bit)类型,其值可以是0或1。 - Andrew Barber
4
除了 @p.s.w.g 提到的内容外,在数据库中存储 this.password.Text 可能不是一个好主意。参见这个问题那个问题以及这篇文章 - O. R. Mapper
1
欲了解更多信息,请观看Hacking Websites with SQL InjectionHow NOT to Store Passwords!,以便初步了解这些问题。 - p.s.w.g
1
@user3186213,这确实很重要,即使只是一份作业!糟糕的生产代码源于从做得不好的作业中获得的不良习惯。你现在学到的东西将贯穿你的整个职业生涯。 - Ondrej Tucny
显示剩余2条评论
4个回答

2

我几年前使用过Access,但只记得Access中的布尔值如下:

0 = false
-1 = true

所以,要通过复选框插入内容,您可以像这样使用:
int val;
    if(CheckBox.Checked == true)
{
val = -1;
}
else
{
val =0
}

那么您应该将它添加到您的查询中:
string write = "Insert into Employees (ID, Name, Last_name, Username, Password, E_mail, Address) values('" + this.ID.Text + "','" + this.name.Text + "','" + this.lastName.Text + "','" + this.userName.Text + "','" + this.password.Text + "','" + this.eMail.Text + "','" + this.address.Text + "'"+val");";

2

对我来说,使用参数更好,虽然我不太喜欢ADO.NET:

SqlCommand Command = new SqlCommand();
            Command.Connection = MyConnection;
            Command.CommandText = "Insert into Employees (ID, Name, Last_name, Username, Password, E_mail, Address, administrator_rights)"
                              + "values(@ID, @Name, @Last_name, @Username, @Password, @E_mail,Address, @admin_rights )";

            Command.Parameters.Add("@ID", 1); // some generated number
            Command.Parameters.Add("@Name", TextBoxName.Text);
            Command.Parameters.Add("@Last_name", TextBoxLastName.Text);
            Command.Parameters.Add("@Username", TextBoxUserName.Text);
            Command.Parameters.Add("@Password", TextBoxPassword.Text);
            Command.Parameters.Add("@E_mail", TextBoxEmail.Text);
            Command.Parameters.Add("@Address", TextBoxAddress.Text);
            Command.Parameters.Add("@admin_rights", CheckBoxAdminRights.Checked);

            using (MyConnection)
            {
                Command.ExecuteNonQuery();
            }

1

SQL Server 2008 R2有一个位类型,可以在布尔情况下使用。

示例表创建脚本:

/****** Object:  Table [dbo].[testTable]    Script Date: 01/12/2014 16:16:18 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[testTable](
    [test] [bit] NOT NULL,
    [testr] [int] NOT NULL
) ON [PRIMARY]

GO

查询插入。
INSERT INTO [dbo].[testTable]
           ([test]
           ,[testr])
     VALUES
           ('false','32')
GO

在C#中的查询示例:

string sqlQuery=@"Insert Into testTable(test,testr) VALUES("'+checkbox.checked+'","'+empId+'")";

在SQL Server中,您不能使用字符串“false”来表示位字段,必须使用1或0。 - Orion

1
使用参数:
OleDbCommand command = new OleDbCommand("Insert into Employees (ID, Name, Last_name, Username, Password, E_mail, Address, administrator_rights) values(?, ?, ?, ?, ?, ?, ?, ?)",
                                        new OleDbConnection ("YourConnection"))
{
    Parameters = { new OleDbParameter("ID", OleDbType.Integer),
                   new OleDbParameter("Name", OleDbType.VarWChar ),
                   new OleDbParameter("Last_name", OleDbType.VarWChar),
                   new OleDbParameter("Username", OleDbType.VarWChar),
                   new OleDbParameter("Password", OleDbType.VarWChar),
                   new OleDbParameter("E_mail", OleDbType.VarWChar),
                   new OleDbParameter("Address", OleDbType.VarWChar),
                   new OleDbParameter("administrator_rights", OleDbType.Boolean )}
};

private bool Update()
{

    command.Parameters["ID"].Value = this.ID.Text;
    command.Parameters["Name"].Value = this.name.Text;
    command.Parameters["Last_name"].Value = this.lastName.Text;
    command.Parameters["Username"].Value = this.userName.Text;
    command.Parameters["Password"].Value = this.password.Text;
    command.Parameters["E_mail"].Value = this.eMail.Text;
    command.Parameters["Address"].Value = this.address.Text;
    command.Parameters["administrator_rights"].Value = checkBox1.Checked;

    if (command.Connection.State != ConnectionState.Open) command.Connection.Close();
    var result =  command.ExecuteNonQuery();
    command.Connection.Close();

    return result == 0 ? false : true;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接