标题:c# 往datagridview添加时限定重复的数据添加
只看楼主
yangjm
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-12-26
 问题点数:0 回复次数:0 
c# 往datagridview添加时限定重复的数据添加
我向datagridview添加数据时由于没有做限制有时候添加进去的数据重复,请各位帮忙我的添加代码如下:
SqlConnection con = new SqlConnection("Server=(local);database=ipdb;Uid=sa;Pwd=1234");
            con.Open();
            SqlCommand cmd = new SqlCommand("ysh", con);  //ysh为创建的存储过程,创建存储过程时要有一定的语法结构
             = CommandType.StoredProcedure;
            SqlParameter[] prams = {
                        new SqlParameter("@ip地址", SqlDbType.VarChar, 30),//初始化类的新实例
                        new SqlParameter("@mac地址", SqlDbType.VarChar, 30),
                        new SqlParameter("@标志位", SqlDbType.Int, 4),
                        new SqlParameter("@用户姓名", SqlDbType.VarChar, 30),
                        new SqlParameter("@电话", SqlDbType.Int, 4),
                        new SqlParameter("@房间号", SqlDbType.VarChar, 30),
                        new SqlParameter("@网段", SqlDbType.VarChar, 30),
                        new SqlParameter("@单位", SqlDbType.VarChar, 30),
                        new SqlParameter("@所在交换机", SqlDbType.VarChar, 30),
                        new SqlParameter("@交换机所在位置", SqlDbType.VarChar, 30),
                        new SqlParameter("@端口", SqlDbType.VarChar, 30),
                        new SqlParameter("@时间", SqlDbType.DateTime, 8),
                        };
            prams[0].Value = textBox1.Text;//获取该参数的值
            prams[1].Value = textBox2.Text;
            prams[2].Value = comboBox2.Text;
            prams[3].Value = textBox4.Text;
            prams[4].Value = textBox5.Text;
            prams[5].Value = textBox6.Text;
            prams[6].Value = comboBox3.Text;
            prams[7].Value = comboBox1.Text;
            prams[8].Value = textBox11.Text;
            prams[9].Value = textBox12.Text;
            prams[10].Value = textBox13.Text;
            prams[11].Value = textBox14.Text;
            
               
                //SelectedCells[0].Value.ToString())
            {
                MessageBox.Show("对不起,您输入的IP地址已经存在,不能再添加数据!");
            }
            else
            {
                // 添加参数
                foreach (SqlParameter parameter in prams)
                {
                    cmd.Parameters.Add(parameter);
                }
                cmd.ExecuteNonQuery();
                con.Close();
               
            }
搜索更多相关主题的帖子: datagridview 数据 con cmd 
2007-12-26 10:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-194355-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.277300 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved