How to insert bool into database

陌路散爱 提交于 2020-07-21 01:25:15

问题


I have to add user data into database table named "employees". It has ID, Name, LastName,UserName, Password, E-mail, address, administrator_rights options.

Administator_rigts is bool option (yes or no).

How do i do that, when i make a form that has all the data, and i want to check if user has administrator rights by checkbox (checked - true , unchecked - false).

This is my statement which doesn't include admin_rights because i dunno how to do it.

Please help me out

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

Thank you all for your responses


回答1:


Using Parameters is better for me , although i don't prefer 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();
            }



回答2:


I used access few years ago but as I can remember boolean value in access was as below:

0 = false
-1 = true

So for insert to by your check box you may use like this:

int val;
    if(CheckBox.Checked == true)
{
val = -1;
}
else
{
val =0
}

then you should add it to your query:

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");";



回答3:


sql server 2008 r2 has a bit type which can b used in case of bool. sample table create script

/****** 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

Query to insert

INSERT INTO [dbo].[testTable]
           ([test]
           ,[testr])
     VALUES
           ('false','32')
GO

WHIle in c# Query sample:

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



回答4:


Use Parameters:

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;
}


来源:https://stackoverflow.com/questions/21073357/how-to-insert-bool-into-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!