如何实现检索数据库中的记录不重复注册

我们常常在使用某个网站时候,要成为该网站会员就需要注册一个账号。往往不允许用户名或账号重复的,因此当新用户注册时。如果遇到用户名或账号相同时,就应该提示用户,该账号用户已经存在。那么这个功能怎么来实现的呢?通常有以下几种技巧:

1、用ExecuteScalar()方法。

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "")
                MessageBox.Show("请填写完整的信息!", "信息提示");
            else
            {
                sql.sqlConnection.Open();
                string sqluser = "select * from userinfo where username='"+textBox1.Text.Trim()+"'";
                SqlCommand MyCom = new SqlCommand(sqluser,sql.sqlConnection);
               
if (null != MyCom.ExecuteScalar())                                                       //检测查询到的结果是否为空,如果不为空则表明该记录存在
                {
                    MessageBox.Show(textBox1.Text.Trim() + "用户名已经存在,请重新注册用户名", "信息提示");
                    textBox1.Text = "";
                    textBox2.Clear();
                    textBox3.Clear();
                    textBox1.Focus();
                }
                else if (textBox2.Text.Trim() == textBox3.Text.Trim())
                {
                    string sqlnewuser = "insert into userinfo(username,userpwd)" + " values('" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "')";
                    MyCom.CommandText = sqlnewuser;
                    MyCom.ExecuteNonQuery();
                    textBox1.Text = "";/*清空文本框*/
                    textBox2.Text = "";
                    textBox3.Text = "";
                    MessageBox.Show("用户添加成功","信息提示");
                    textBox1.Focus();//焦点显示到用户名处

                }
                else
                {
                    MessageBox.Show("两次输入的密码不一致!","信息提示");
                    textBox3.Text = "";
                    textBox3.Focus();//焦点显示到确认密码处
                }
                sql.sqlConnection.Close();
            }
           
        }

2、采用DataSet检测数据表中记录是否存在

      public DataSet GetList(string strWhere)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select Id,Symbol_Primary,Name_Primary ");
   strSql.Append(" FROM TB_FileClassificationDirectoryone ");
   if(strWhere.Trim()!="")
   {
    strSql.Append(" where "+strWhere);
   }
   return DBHelp.Query(strSql.ToString());
  }

**************************************************************************************************

 public static DataSet Query(string SQLString)//执行语句
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
                return ds;
            }
        }

*****************************************************************************************************

  public bool addlist1(string name, string num)
        {
            DataSet ds = S_TB_FileClassificationDirectoryone.GetList("Symbol_Primary='"+ num+ "'");
            if (ds.Tables[0].Rows.Count == 0)   //检测数据表中的记录是否存在,等于0表示为空,不存在该记录。
            {
                EModel.Name_Primary = name;
                EModel.Symbol_Primary = num;
                S_TB_FileClassificationDirectoryone.Add(EModel);//添加记录
                return true;
            }
            else
                return false;
        }

******************************************************************************************************

  public decimal Add(Entity.TB_FileClassificationDirectoryone model)
  {
   StringBuilder strSql=new StringBuilder();
   StringBuilder strSql1=new StringBuilder();
   StringBuilder strSql2=new StringBuilder();
   if (model.Symbol_Primary != null)
   {
    strSql1.Append("Symbol_Primary,");
   ********省略

   }
   strSql.Append("insert into TB_FileClassificationDirectoryone(");
   strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
   strSql.Append(")");
   strSql.Append(" values (");
   strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
   strSql.Append(")");
   strSql.Append(";select @@IDENTITY");
   object obj = DBHelp.GetSingle(strSql.ToString());/// 执行一条计算查询结果语句,返回查询结果(object)。

   if (obj == null)
   {
    return 0;
   }
   else
   {
    return Convert.ToDecimal(obj);
   }
  }

*****************************************************************************

        /// <summary>
        /// 执行一条计算查询结果语句,返回查询结果(object)。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果(object)</returns>
        public static object GetSingle(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        object obj = cmd.ExecuteScalar();
                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                        {
                            return null;
                        }
                        else
                        {
                            return obj;
                        }
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        connection.Close();
                        throw e;
                    }
                    finally
                    {
                        cmd.Dispose();
                        connection.Close();
                    }
                }
            }
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值