【问题标题】:How to insert bool into database如何将bool插入数据库
【发布时间】:2014-01-12 09:58:37
【问题描述】:

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

Administator_rigts 是布尔选项(是或否)。

当我制作一个包含所有数据的表单时,我该怎么做,并且我想通过复选框检查用户是否具有管理员权限(选中 - 真,未选中 - 假)。

这是我的声明,其中不包括 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 + "');";

谢谢大家的回复

【问题讨论】:

  • 您的代码极易受到SQL injection 攻击。帮自己(和您的用户)一个忙,并使用准备好的语句。
  • 什么数据库? SQL Server 没有bool 类型。不过它确实有一个位类型,其值可以是 0 或 1。
  • 除了@p.s.w.g 所说的之外,像这样在数据库中存储this.password.Text 可能不是一个好主意。参见this questionthat questionthis article
  • 如需更多信息,请观看Hacking Websites with SQL InjectionHow NOT to Store Passwords! 对这些问题的介绍性讨论。
  • @user3186213 这确实真的很重要,甚至认为它只是一个家庭作业!糟糕的生产代码产生是由于家庭作业做得不好而养成的坏习惯。你现在所学的,你将在你的职业生涯中遵循。

标签: c# database ms-access boolean


【解决方案1】:

我几年前使用过 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();
                }
    

    【讨论】:

      【解决方案3】:

      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# 查询示例中的 WHIle:

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

      【讨论】:

      • 您不能在 SQL Server 中对位字段使用字符串 'false'。你必须使用 1 或 0。
      【解决方案4】:

      使用参数:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-26
        • 1970-01-01
        • 2011-08-06
        • 2012-12-20
        • 2015-12-09
        • 2019-10-25
        • 1970-01-01
        相关资源
        最近更新 更多