注册 登录
编程论坛 VB.NET论坛

c# 往datagridview添加时限定重复的数据添加

yangjm 发布于 2007-12-26 10:57, 1468 次点击
我向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();
               
            }
0 回复
1