写个Exist(username) return bool
判断是否存在该用户
如果存在 提示一下。
这里用AJAX比较好

解决方案 »

  1.   

    插入数据之前按名称select count(*) 一下,大于 0 就是占用了.提示一下,不做保存小心大小写和空格
      

  2.   


    public bool IsExist()
    {
    string name =this.ddl_mname.SelectedItem.Value.ToString();
    string strSQL = "select  *   from   user_info where  Name='"+name+"'"; try
    {
    ExecuteSqlValue(strSQL);
    return true;
    }
    catch
    {
    return false;
    }
    }
    protected static int ExecuteSqlValue(string strSQL)
    {
    string strConn= ConfigurationSettings.AppSettings["datasource"]; SqlConnection myCn = new SqlConnection(strConn);
    SqlCommand myCmd = new SqlCommand(strSQL,myCn);
    try
    {
    myCn.Open();
    object r = myCmd.ExecuteScalar();
    if(Object.Equals(r,null))
    {
    throw new Exception("value unavailable!");
    }
    else
    {
    return (int)r;
    }
    }
    catch(System.Data.SqlClient.SqlException e)
    {
    throw new Exception(e.Message);
    }
    finally
    {
    myCmd.Dispose();
    myCn.Close();
    }
    }
      

  3.   

    太感谢各位了~特别是6楼的laoyingisme 大哥真的太好人了代码都写出来了~太谢谢了
      

  4.   

    -- 判断用户是否存在
    create procedure p_existsUser @userName nvarchar(50)
    as
    declare @value int
    if (select count(1) from tableName where userName = @userName) > 0
    begin
    set @value = 1
    end
    else
    begin
    set @value = 0
    endreturn @valuego-------------
    // 判断用户是否存在
    provite bool bExistsUser(string strUserName)
    {SqlParameter[]par = new SqlParameter[1];reutrn Convert.toBoolen(DbHelperSQL.RunProcedure("p_existsUser", para);
    }-------------        /// <summary>
            /// 执行存储过程,返回影响的行数
            /// </summary>
            /// <param name="storedProcName">存储过程名</param>
            /// <param name="parameters">存储过程参数</param>
            /// <param name="rowsAffected">影响的行数</param>
            /// <returns></returns>
            public static int RunProcedure(string storedProcName, IDataParameter[] parameters)
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    int result;
                    connection.Open();
                    SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
                    result = (int)command.Parameters["ReturnValue"].Value;
                    //Connection.Close();
                    return result;
                }
            }
    -----------
      

  5.   

    顶,哎来晚了。楼上的说得很详细了,就是用新的name为条件写个sql去数据库验证一下重复。