如何将布尔值插入数据库 [英] How to insert bool into database
问题描述
我必须将用户数据添加到名为员工"的数据库表中.它具有ID,名称,姓氏,用户名,密码,电子邮件,地址,administrator_rights选项.
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是bool选项(是或否).
Administator_rigts is bool option (yes or no).
当我创建一个包含所有数据的表单时,我想通过复选框(选中-true,未选中-false)检查用户是否具有管理员权限,该怎么做.
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).
这是我的声明,其中不包含admin_rights,因为我不知道该怎么做.
This is my statement which doesn't include admin_rights because i dunno how to do it.
请帮帮我
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 + "');";
谢谢大家的答复
推荐答案
使用参数对我来说更好,尽管我不喜欢ADO.NET:
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();
}
这篇关于如何将布尔值插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!