2

我以前使用此代码进行插入操作,但同时我想保存单选按钮值:

cmd.CommandText="INSERT INTO Table1 (username,password,gender VALUES(@username,@password,gender)";
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);

我有两个用于男性和女性的单选按钮,我只想在数据库中保存一个值,所以我使用 groupbox 只选择一个单选按钮,但如何以男性或女性的身份插入数据库。

我正在处理 Windows 窗体。

4

5 回答 5

4

怎么样:


cmd.CommandText="INSERT INTO Table1 (username,password,gender) VALUES (@username,@password,@gender)";
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);

if(radioMale.Checked)
    cmd.Parameters.AddWithValue("@gender", "Male");
else
    cmd.Parameters.AddWithValue("@gender", "Female");

于 2012-12-18T18:48:38.673 回答
1

另一种选择是使用一个包含 2 个值的组合框,一个用于男性,一个用于女性,然后插入选定的值。这也会在可能有用或可能没用的表单上占用更少的空间。

cmd.Parameters.AddWithValue("@gender", genderCombo.SelectedValue);
于 2012-12-18T18:56:10.357 回答
0

试试这个

cmd.CommandText="INSERT INTO Table1 (username,password,gender) VALUES (@username,@password,@gender)";
cmd.Parameters.AddWithValue("@username", textBox1.Text);
cmd.Parameters.AddWithValue("@password", textBox2.Text);

if(radioMale.Checked)
    cmd.Parameters.AddWithValue("@gender", "Male");
else
    cmd.Parameters.AddWithValue("@gender", "Female");
于 2013-10-30T12:26:37.670 回答
0
 private string selectedValue, date;
        private int i;
        private SqlCommand xcmd;
        private SqlConnection xcon;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                day_DropDownList.Items.Insert(0, new ListItem("DD", "DD"));
                month_DropDownList.Items.Insert(0, new ListItem("MM", "MM"));
                year_DropDownList.Items.Insert(0, new ListItem("YY", "YY"));
                for (i = 1; i < 32; i++)
                {
                    day_DropDownList.Items.Add(i.ToString());
                }
                for (i = 1; i < 13; i++)
                {
                    month_DropDownList.Items.Add(i.ToString());
                }
                for (i = 1950; i < 2014; i++)
                {
                    year_DropDownList.Items.Add(i.ToString());
                }
                employeeName_Txt.Focus();
            }


        }



        protected void submit_Button_Click(object sender, EventArgs e)
        {
            selectedValue = gender_RadioButtonList.SelectedValue;

            xcon = new SqlConnection("Data Source=.; DataBase=AptechDB; UID=sa; Password=123;");

            xcon.Open();
            date = day_DropDownList.Text.ToString() + "/" + month_DropDownList.Text.ToString() + "/" + year_DropDownList.Text.ToString();
            xcmd = new SqlCommand("insert into tblEmployee values('" + employeeName_Txt.Text + "','" + date + "','" + selectedValue + "','" + post_Txt.Text + "','" + city_Txt.Text + "','" + country_Txt.Text + "','" + mobileno_Txt.Text + "')", xcon);
            xcmd.ExecuteNonQuery();
            Label1.Text = "Information submitted successfully";
            xcon.Close();
            clear();
        }

        public void clear()
        {
            employeeName_Txt.Text = "";
            day_DropDownList.SelectedIndex = 0;
            month_DropDownList.SelectedIndex = 0;
            year_DropDownList.SelectedIndex = 0;
            post_Txt.Text = "";
            city_Txt.Text = "";
            country_Txt.Text = "";
            mobileno_Txt.Text = "";
        }

    }
}
于 2013-12-01T05:47:14.617 回答
-1
        SqlCommand cmd = new SqlCommand("insert into table1 (Name,Mail,Phone,Addres,Gender) values (@Name,@Mail,@Phone,@Addres,@Gender)", con);
        cmd.Parameters.AddWithValue("@Name", textBox1.Text);
        cmd.Parameters.AddWithValue("@Mail", textBox2.Text);
        cmd.Parameters.AddWithValue("@Phone", textBox3.Text);
        cmd.Parameters.AddWithValue("@Addres", textBox4.Text);
        if (radioButton1.Checked==true)
            cmd.Parameters.AddWithValue("@gender", "Male");
        else
            cmd.Parameters.AddWithValue("@gender", "Female");
于 2015-11-24T06:50:55.543 回答