如何将bool插入数据库

时间:2014-01-12 09:58:37

标签: c# database ms-access boolean

我必须将用户数据添加到名为“employees”的数据库表中。它有ID,Name,LastName,UserName,Password,E-mail,address,administrator_rights选项。

Administator_rigts是bool选项(是或否)。

当我制作包含所有数据的表单时,如何执行此操作,并且我想通过复选框检查用户是否具有管理员权限(选中 - true,unchecked - 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 + "');";

感谢大家的回复

4 个答案:

答案 0 :(得分: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 :(得分:1)

几年前我使用过访问权限,但我记得访问中的布尔值如下:

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 :(得分:1)

sql server 2008 r2有一个位类型,可以在bool的情况下使用。 样本表创建脚本

/****** 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+'")";

答案 3 :(得分: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;
}