如何在连接模式下将数据从组合框插入sql数据库? [英] how to insert data from combo box to sql database in connected mode?

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

问题描述

我的代码有问题.........

I have some problem in this code............

cmd.Parameters.Add(new SqlParameter("@dept",comboBox_dept.SelectedValue));

推荐答案

问题中没有提到什么错误发生在使用上面的代码.但是,我认为最好使用
It is not mentioned in the question what error occurred in using the above code. However, I think it is better to use
command.Parameters.AddWithValue("@dept", comboBox_dept.SelectedValue);


如此处所述
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx [ ^ ]


as explained here
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx[^]



检查此
Hi ,
Check this
private void button1_Click(object sender, EventArgs e)
     {

         using (SqlConnection con =
                new SqlConnection(@"Data Source=IT-DEV2\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"))
         {
             con.Open();
             using (SqlCommand cmd = new SqlCommand("INSERT INTO dbo.cate (cate_id) values (@cate_id )", con))
             {
                 cmd.Parameters.AddWithValue("@cate_id", comboBox1.SelectedValue );

                 cmd.ExecuteNonQuery();
             }
         }

     }



最好的问候
M.Mitwalli



Best Regards
M.Mitwalli


您组合了两个步骤:
You combined two steps:
cmd.Parameters.Add(new SqlParameter("@dept",SqlDbType.Int));
command.Parameters["@dept"].Value = comboBox_dept.SelectedValue;


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

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