如何使用组合框将数据插入数据库 [英] how to insert data into database using combo box

查看:93
本文介绍了如何使用组合框将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想使用按钮之类的事件处理程序将数据插入数据库列中
我已插入文本框的文本,但我不知道combobox
有人可以帮我吗
我在这里有插入代码

i just want to insert a data into database column using event handler like a button
i have inserted a text box''s text but i dont know about combobox
can anybody help me out
i have my insertion code here

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\aquib\my documents\visual studio 2010\Projects\login\login\Database1.mdf;Integrated Security=True;User Instance=True";
            string q="insert into Table1 values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"')";
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("suceed");
        }


我只想在此处添加一个组合框的数据以添加到数据库列中
预先感谢


i just want to add one combo box''s data here to add in database column
thanks in advance

推荐答案

首先,不要将值直接连接到SQL语句中.这将使您易于进行SQL注入,并可能导致数据类型转换问题等等.而是使用 SqlParameter [ SelectedValue [ ^ ]假定您已定义 ValueMember [ ^ ]
First of all, don''t concatenate the values into your SQL statement directly. This will leave you open to SQL injections and may cause data type conversion problems and so on. Instead use the SqlParameter[^].

About the actual question, you can use the SelectedValue[^] of the combobox to retrieve the value behind the selection, taken that you have defined the ValueMember[^]


只需在
textBox1.Text

comboBox1.Items[0].ToString()


其中items [0]表示ComboBox中的第一个值,您可以将其替换为第二个值items [1] ... etc>>祝您好运


where items[0] mean the first value in ComboBox and you can replace it with the second value items[1] ...etc >> good luck


就像Textbox一样,您可以使用textbox1.Text获取txt.对于combobox,您需要获取selectedindex,然后获取文本

试试这个..

Just like for Textbox you get the txt using textbox1.Text, for combobox, you need to get the selectedindex and then get the text

Try this..

private void button1_Click(object sender, EventArgs e)
        {
            string s = "";
             if(comboBox1.SelectedIndex>=0)
                 s = comboBox1.Items[comboBox1.SelectedIndex].ToString();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\aquib\my documents\visual studio 2010\Projects\login\login\Database1.mdf;Integrated Security=True;User Instance=True";
            string q="insert into Table1 values(''"+textBox1.Text+"'',''"+textBox2.Text+"'',''"+s+"'')";
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("suceed");
        }



在这里,我用combox所选项目替换了最后一个参数.

希望这会有所帮助.
欢呼



Here I replaced the last parameter with combox selected item.

Hope this helps.
cheers


这篇关于如何使用组合框将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆