C# 如何通过单选按钮在sql server中插入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13939411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:09:09  来源:igfitidea点击:

how to insert values in sql server through radio button

c#winforms

提问by shariq_khan

I previously use this code for insert operation but along with this I want to save the radiobutton values:

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

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

I have two radio buttons for male and female, I want to save only one value in the database so I have used groupbox for selecting only one radio button but how to insert into database as male or female.

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

I am working on windows form.

我正在处理 Windows 窗体。

采纳答案by Trevor Pilley

Another option is to use a combo box with 2 values in it, one for male and one for female and just insert the selected value. This would also take up less space on the form which may or may not be useful.

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

cmd.Parameters.AddWithValue("@gender", genderCombo.SelectedValue);

回答by tucaz

How about:

怎么样:


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

回答by Trevor Pilley

Try this one

试试这个

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

回答by Trevor Pilley

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

    }
}

回答by Anil

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