2014-02-07 106 views
0

我正在构建一个C#Windows应用程序,其中包含两个用于性别的单选按钮。我正在使用一个简单的插入查询在数据库中插入数据。将单选按钮选定值插入到数据库中

任何人都可以帮助我将选定的单选按钮值插入数据库?我使用2个单选按钮,而不是单选按钮列表。

就目前我的查询如下:

Class1.ABC("insert into emp(code,names,m_name,desgnation,gender) 
         values ('" + textBox1.Text + "','" + 
            textBox2.Text + "','" + 
            textBox3.Text + "','" + 
            textBox4.Text + "','" + 
            textBox5.Text + "','" + 
            radioButton1.Checked+"')"); 
+0

它究竟是如何失败的?另外,您展示的代码是SQL注入漏洞的主要候选对象。你不想拥有“汤米表”作为用户。 http://xkcd.com/327/ – ThatBlairGuy

+1

@Srima你指定了五列,但你传递了六个值。可能是这个问题? –

+0

@ Selman22 - 我认为你已经钉住了它,至少在展示的例子中。 – ThatBlairGuy

回答

0

您可以检查checked属性和设定值按您的要求。

Class1.ABC("insert into emp(code,names,m_name,desgnation,gender) 
         values ('" + textBox1.Text + "', 
           '" + textBox2.Text + "', 
           '" + textBox3.Text + "', 
           '" + textBox4.Text + "', 
           '" + textBox5.Text + "', 
           '"+ radioButton1.Checked ? "Male" : "Female" +"')"); 

注意:使用parameterized queries

+0

我有2个单选按钮。那我该怎么写呢?感谢您的合作 – Srima

+0

在一个面板或组框中添加单选按钮。当他们在一个容器中多于一个单选按钮时,只有一个被选中。所以如果'radioButton1'被选中,另一个不是。 – Sameer

-1

你可以试试这个:

添加标签,使其invisible.get选中的单选按钮的值到标签。现在在sql语句中,使用label的值(实际上是checked radiobutton的值)插入到'gender'中。 我推荐这只有当你是初学者&无论如何想让程序运行。

+1

这是回答这个问题吗? –

+0

Ofcourse。您不必从检查的单选按钮中获取值,而是从标签或文本框中获取值,该值实际上是检查的单选按钮的值。 – Sumedh

+0

您需要确定答案和评论之间的区别!看看[我如何写出一个好的答案?](http://stackoverflow.com/help/how-to-answer) –

0

只需Inser单选按钮文本

像这样

Class1.ABC("insert into emp(code,names,m_name,desgnation,gender) 
         values ('" + textBox1.Text + "','" + 
            textBox2.Text + "','" + 
            textBox3.Text + "','" + 
            textBox4.Text + "','" + 
            textBox5.Text + "','" + 
            radioButton1.Text + "')"); 
0
private string RadioButtonText() 
{ 
    if (radioButton1.Checked) 
    { 
     return radioButton1.Text; 
    } 
    else 
    { 
     return radioButton2.Text; 
    } 
} 
相关问题