在c#.net中的数据库中插入单选按钮值

时间:2014-01-17 05:35:01

标签: c#

private void btnUpdate_Click(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection(constr);
    con.Open();
    if (radioButton1.Checked)
    {
        string Sql_radio = "Insert Into tb1(name)Values ('Yes')";
    }

    if (radioButton2.Checked)
    {
        string Sql_radio = "Insert Into tb1(name)Values ('no')";
    }

    OleDbCommand cmd = new OleDbCommand(Sql_radio, con);


    cmd.ExecuteNonQuery();
    con.Close();
    MessageBox.Show("Inserted sucessfully");
}

你好..我正在开发一个winform应用程序,现在我有两个单选按钮,你可以在上面的代码中看到。

我希望如果用户selects radiobutton1然后yes应该插入数据库,如果用户selects radiobutton2那么它应该在数据库中insert no

我已经应用了以下条件,但它给出了错误

  

名称Sql_radio在当前上下文中不存在

任何人都可以告诉我正确的方法来执行上述代码吗?

5 个答案:

答案 0 :(得分:0)

您必须在Sql_radio函数的代码中全局声明字符串btnUpdate_Click

见以下内容:

private void btnUpdate_Click(object sender, EventArgs e)
    {
         string Sql_radio="";

        OleDbConnection con = new OleDbConnection(constr);
        con.Open();
        if (radioButton1.Checked)
        {

            Sql_radio = "Insert Into tb1(name)Values ('Yes')";

        }
        if (radioButton2.Checked)
        {

            Sql_radio = "Insert Into tb1(name)Values ('no')";

        }
         OleDbCommand cmd = new OleDbCommand(Sql_radio, con);


        cmd.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Inserted sucessfully");

    }

答案 1 :(得分:0)

string Sql_radio

中声明If
Try
{
    OleDbConnection con = new OleDbConnection(constr);
    con.Open();
    string Sql_radio = ""; 
    if(RadioButton1.Checked)
    {
     Sql_radio = "Insert Into tb1(name)Values ('Yes')";
    }
    else if(RadioButton2.Checked)
    {
     Sql_radio = "Insert Into tb1(name)Values ('No')";
    }
    OleDbCommand cmd = new OleDbCommand(Sql_radio, con);


    cmd.ExecuteNonQuery();
    con.Close();
    MessageBox.Show("Inserted sucessfully");
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}

答案 2 :(得分:0)

private void btnUpdate_Click(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection(constr);
    con.Open();
    string Sql_radio = "";
    if (radioButton1.Checked)
    {
        Sql_radio = "Insert Into tb1(name)Values ('Yes')";
    }
    if (radioButton2.Checked)
    {
        Sql_radio = "Insert Into tb1(name)Values ('no')";
    }
    OleDbCommand cmd = new OleDbCommand(Sql_radio, con);

    cmd.ExecuteNonQuery();
    con.Close();
    MessageBox.Show("Inserted sucessfully");
}

Sql_radio片段之后if的范围已过期。将声明移动到方法范围,一切都会好的。

答案 3 :(得分:0)

if condition

之外声明您的字符串
    private void btnUpdate_Click(object sender, EventArgs e)
{

    OleDbConnection con = new OleDbConnection(constr);
    con.Open();
    string Sql_radio = String.Empty;
    if (radioButton1.Checked)
    {

         Sql_radio = "Insert Into tb1(name)Values ('Yes')";

    }
    if (radioButton2.Checked)
    {

        Sql_radio = "Insert Into tb1(name)Values ('no')";

    }
     OleDbCommand cmd = new OleDbCommand(Sql_radio, con);


    cmd.ExecuteNonQuery();
    con.Close();
    MessageBox.Show("Inserted sucessfully");

}

答案 4 :(得分:0)

字符串Sql_radio 放在if条件之外,如下所述:

private void btnUpdate_Click(object sender, EventArgs e)
{
    var con = new OleDbConnection(constr);
    con.Open();

    var Sql_radio = string.Empty;

    if (radioButton1.Checked)
    {
        Sql_radio = "Insert Into tb1(name)Values ('Yes')";
    }

    if (radioButton2.Checked)
    {
        Sql_radio = "Insert Into tb1(name)Values ('no')";
    }

    var cmd = new OleDbCommand(Sql_radio, con);

    cmd.ExecuteNonQuery();
    con.Close();
    MessageBox.Show("Inserted sucessfully");
}