SQL数据库插入数据判断是否有重复出现

 

 

c#中判断插入重复使用的sql语句是select count(*) from DBName where 列名="输入内容",

这里返回的值是一个int型的.因此需要一个int型的变量接收.通过判断count的值决定要否插入.

如果为0,则可以,若非0,则重新输入值.以下是我在使用过程中的一段代码:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void btOK_Click(object sender, EventArgs e)
        {
            DataConn objDC = new DataConn();
            string strJudge = "select count(*) from SYS.SYSTEMUSER where password='" + this.tbPassword.Text + "'";
            DataTable dtCount = objDC.ConnTable(strJudge);
            int cnt = int.Parse(dtCount.Rows[0][0].ToString());
            
            if (cnt == 0)
            {
                string strUpdate = "Update SYS.SYSTEMUSER set PASSWORD = '" + this.tbPassword.Text + "'," + " AUTHORITY = '"
                               + this.tbAuthor.Text + "'" + "where USERID= '" + this.tbUserName.Text + "'";
                objDC.UpdateData(strUpdate);
                this.Close();
            }
            else
            {
                MessageBox.Show("该用户名已经存在,请重新输入");
            }  
        }

posted on 2014-01-15 15:09  _尼欧  阅读(1349)  评论(4)    收藏  举报

导航