【问题标题】:Select value of specific membership profile property using SQL query使用 SQL 查询选择特定会员资料属性的值
【发布时间】:2014-05-16 19:36:34
【问题描述】:

尽管这应该是一个常见问题,但我在 Google 中并没有找到太多相关信息。 我正在使用 ASP.NET 成员资格,并将所有用户属性保存在 aspnet_Profile 表的 PropertyValuesString 字段中。

现在我想通过特定属性值搜索用户,但使用 SQL 查询。最好的性能明智的方法是什么?

【问题讨论】:

    标签: asp.net sql-server asp.net-membership membership-provider


    【解决方案1】:

    asp_profile 的东西是这样一个字符串连接黑客工作,这非常困难。

    但是你去吧。

    http://www.karpach.com/Get-asp-net-profile-value-MS-SQL-database-using-T-SQL.htm

    我将在此处粘贴代码以避免死链接问题。但也请查看链接,因为有些 cmets 值得一读。

    ASP.Net profiles can store binary data as well, but usually your are interested in string data such as First and Last names. First lets create helper function, which helps to get position:length pair values:
    
    CREATE FUNCTION dbo.fn_GetElement
    
    (
    
    @ord AS INT,
    
    @str AS VARCHAR(8000),
    
    @delim AS VARCHAR(1) )
    
    
    
    RETURNS INT
    
    AS
    
    BEGIN
    
      -- If input is invalid, return null.
    
      IF @str IS NULL
    
          OR LEN(@str) = 0
    
          OR @ord IS NULL
    
          OR @ord < 1
    
          -- @ord > [is the] expression that calculates the number of elements.
    
          OR @ord > LEN(@str) - LEN(REPLACE(@str, @delim, '')) + 1
    
        RETURN NULL
    
      DECLARE @pos AS INT, @curord AS INT
    
      SELECT @pos = 1, @curord = 1
    
      -- Find next element's start position and increment index.
    
      WHILE @curord < @ord
    
        SELECT
    
          @pos    = CHARINDEX(@delim, @str, @pos) + 1,
    
          @curord = @curord + 1
    
      RETURN
    
      CAST(SUBSTRING(@str, @pos, CHARINDEX(@delim, @str + @delim, @pos) - @pos) AS INT)
    
    END
    
    
    
    And then code for the actual worker function:
    
    CREATE FUNCTION dbo.fn_GetProfileElement
    
    (
    
    @fieldName AS NVARCHAR(100),
    
    @fields AS NVARCHAR(4000),
    
    @values AS NVARCHAR(4000))
    
    
    
    RETURNS NVARCHAR(4000)
    
    AS
    
    BEGIN
    
      -- If input is invalid, return null.
    
      IF @fieldName IS NULL
    
          OR LEN(@fieldName) = 0
    
          OR @fields IS NULL
    
          OR LEN(@fields) = 0
    
          OR @values IS NULL
    
          OR LEN(@values) = 0
    
    
    
        RETURN NULL
    
    
    
    -- locate FieldName in Fields
    
    DECLARE @fieldNameToken AS NVARCHAR(20)
    
    DECLARE @fieldNameStart AS INTEGER,
    
    @valueStart AS INTEGER,
    
    @valueLength AS INTEGER
    
    
    
    -- Only handle string type fields (:S:)
    
    SET @fieldNameStart = CHARINDEX(@fieldName + ':S',@Fields,0)
    
    
    
    -- If field is not found, return null
    
    IF @fieldNameStart = 0 RETURN NULL
    
    SET @fieldNameStart = @fieldNameStart + LEN(@fieldName) + 3
    
    
    
    -- Get the field token which I've defined as the start of the
    
    -- field offset to the end of the length
    
    SET @fieldNameToken = SUBSTRING(@Fields,@fieldNameStart,LEN(@Fields)-@fieldNameStart)
    
    
    
    -- Get the values for the offset and length
    
    SET @valueStart = dbo.fn_getelement(1,@fieldNameToken,':')
    
    SET @valueLength = dbo.fn_getelement(2,@fieldNameToken,':')
    
    
    
    -- Check for sane values, 0 length means the profile item was
    
    -- stored, just no data
    
    IF @valueLength = 0 RETURN ''
    
    
    
    -- Return the string
    
    RETURN SUBSTRING(@values, @valueStart+1, @valueLength)
    
    
    
    END
    
    
    
    Now we can get first name and last name as following:
    
    
    
    SELECT dbo.fn_GetProfileElement('FirstName',PropertyNames,PropertyValuesString) + ' ' +
    
    
    
    dbo.fn_GetProfileElement('LastName',PropertyNames,PropertyValuesString) as FullName FROM aspnet_Profile
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 2011-06-28
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    相关资源
    最近更新 更多