【问题标题】:How to insert data from radiobutton and combobox to table in MS Sql如何将数据从单选按钮和组合框插入 MS Sql 中的表
【发布时间】:2014-05-10 09:36:57
【问题描述】:

我正在使用 c# WF 构建应用程序。我在 MS sql 数据库中创建了一个员工表。我有两个性别单选按钮(男性和女性)。根据用户单击单选按钮(男性或女性)的方式,我想编写可以插入两个单选按钮选项之一的 sql 语句。同样的事情也适用于组合框。在用户选择从组合框中选择数据后,我希望将数据保存在表中。我用谷歌搜索了这些问题,但没有找到正确的问题。 2 月 7 日在这里发布了一个。问题无人回答。不知道如何为组合框编写代码。

("INSERT INTO Employeess(EmpID,FirstName,LastName,Salary,desgnation,gender) 
                        values ('" + textBox1.Text + "','" + 
                                     textBox2.Text + "','" + 
                                     textBox3.Text + "','" + 
                                     textBox4.Text + "','" + 
                                     textBox5.Text + "','" + 
                                     **radioButton1.Checked+"'** )");

非常感谢任何帮助。

【问题讨论】:

  • gender的列数据类型是什么?
  • 用户在数据库中保存一个标志/ID....
  • 列数据类型为varchar(6)。

标签: c# sql combobox insert radio-button


【解决方案1】:

为您的列使用位数据类型。然后可以直接使用 SQL 参数插入radioButton1.Checked 检查值。

由于您没有提供完整代码,请尝试

("INSERT INTO Employeess(EmpID,FirstName,LastName,Salary,desgnation,gender) 
                        values ('" + textBox1.Text + "','" + 
                                     textBox2.Text + "','" + 
                                     textBox3.Text + "','" + 
                                     textBox4.Text + "','" + 
                                     textBox5.Text + "'," + 
                                      (radioButton1.Checked ? "1" : "0") +" )");

【讨论】:

    【解决方案2】:

    我完全改变了我的代码并且它可以工作。我正在使用 sqlcommand.paramemters.addwithvalue。当我搜索解决方案时,我发现以前的编码对于 sql 注入是很脆弱的。感谢您的帮助。以下是我将数据从文本框、组合框和单选按钮保存到数据库的完整代码。

        private void btnSave_Click(object sender, EventArgs e)
        {
    
            try
            {
    
    
                DataValidateAndDateFormat();
    
                string strGender;
                string strConnectionString = @"Data Source = KK\SQLEXPRESS; Integrated Security = SSPI; Initial Catalog = JeanDB";
    
                SqlConnection cn = new SqlConnection(strConnectionString);
                cn.Open();
    
                string strEmpID = txtEmpID.Text.Trim();
                string strFirstName = txtFirstName.Text.Trim();
                string strLastName = txtLastName.Text.Trim();
                string strDesignation = txtDesignation.Text.Trim();
                int iSalary = Convert.ToInt32(txtSalary.Text.Trim());
                string strAddress = txtAddress.Text.Trim();
                int iZipCode = Convert.ToInt32(txtZipCode.Text.Trim());
                int iPhone = Convert.ToInt32(txtPhone.Text.Trim());
                string strEmail = txtEmail.Text.Trim();
                DateTime dtDOB = dtPickerDOB.Value;
                string strNationality = comboNationality.SelectedItem.ToString();
    
                if (rbMale.Checked)
                    strGender = "Male";
                else
                    strGender = "Female";
    
                string strUserName = txtUserName.Text.Trim();
                string strPassword = txtPassword.Text.Trim();
    
                string query = "INSERT INTO Employees(EmployeeID, FirstName, LastName, Designation, Salary, Address, ZipCode, Phone, Email, DOB, Nationality, Gender, Username, Password)VALUES(@strEmpID, @strFirstName, @strLastName, @strDesignation, @iSalary, @strAddress, @iZipCode, @iPhone,@strEmail, @dtDOB, @strNationality, @strGender, @strUserName, @strPassword)";
                SqlCommand InsertCommand = new SqlCommand(query, cn);
                InsertCommand.Connection = cn;
    
                InsertCommand.Parameters.AddWithValue(@"strEmpID", strEmpID);
                InsertCommand.Parameters.AddWithValue(@"strFirstName", strFirstName);
                InsertCommand.Parameters.AddWithValue(@"strLastName", strLastName);
                InsertCommand.Parameters.AddWithValue(@"strDesignation", strDesignation);
                InsertCommand.Parameters.AddWithValue(@"iSalary", iSalary);
                InsertCommand.Parameters.AddWithValue(@"strAddress", strAddress);
                InsertCommand.Parameters.AddWithValue(@"iZipCode", iZipCode);
                InsertCommand.Parameters.AddWithValue(@"iPhone", iPhone);
                InsertCommand.Parameters.AddWithValue(@"strEmail", strEmail);
                InsertCommand.Parameters.AddWithValue(@"dtDOB", dtDOB);
                InsertCommand.Parameters.AddWithValue(@"strNationality", strNationality);
                InsertCommand.Parameters.AddWithValue(@"strGender", strGender);
                InsertCommand.Parameters.AddWithValue(@"strUsername", strUserName);
                InsertCommand.Parameters.AddWithValue(@"strPassword", strPassword);
    
                InsertCommand.ExecuteNonQuery();
                MessageBox.Show("New Employee's Data has been added successfully");
    
                cn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      • 2022-01-22
      相关资源
      最近更新 更多