How to get asp.net profile value from MS SQL database using T-SQL?

The aspnet_Profile table contains the following fields: UserId, PropertyNames, PropertyValuesString, PropertyValuesBinary,  and LastUpdatedDate. The PropertyNames field contains a string delimited with colons (:) that identify which profile fields are stored, what their datatype is and their offset and length.

For Example:

FirstName:S:1:7:PostalCode:S:8:5:Street:S:13:15:LastName:S:28:7:

Actual values stored in PropertyValuesString:

Viktar 601481336 Finley rd Karpach

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

 

 Special thanks to the Richard Edwards at DNNStuff


Sunday, June 29, 2008 | Comments (13) | Add Comment

Comments

Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Very nice! That's exactly what I was looking for.

7/21/2008 7:58:15 AM | by Valera
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Thanks!!!.. =) is a very good !!

8/5/2008 9:36:16 AM | by Augusto
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Thanks ....Very Very nice

8/13/2008 3:36:27 AM | by ThanhTung
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Thanks!

9/24/2008 8:47:38 AM | by BobC
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Thank You! I've been busting my brains out trying to get a dll that performs the same functions as ProfileCommon for a console application. No luck there. And I didn't want to program an interface. This was a great shortcut!

2/10/2009 7:57:14 PM | by Nick S.
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Thank you! It's cool (_)]

2/18/2009 12:31:25 AM | by telz
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Great blog
Thanks a lot, very useful

6/5/2009 3:42:45 AM | by Wallace740
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

A very lucky day for me!
I found such a useful script!

6/5/2009 1:54:51 PM | by Atip C
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Here's another flavor of it that I made. Just uses a single function.

CREATE FUNCTION [dbo].[GetProperty]
(
@UserId uniqueidentifier,
@PropertyName varchar(50)
)
RETURNS varchar(500)
AS
BEGIN
DECLARE @ret varchar(500)
DECLARE @PropPos int, @Names varchar(500), @Values varchar(500), @start tinyint, @length tinyint

SELECT @Names = PropertyNames, @Values = PropertyValuesString FROM aspnet_Profile WHERE UserId = @UserId
SELECT @PropPos = (PATINDEX('%'+ @PropertyName +':S%', @Names) + LEN(@PropertyName) + 3)
SELECT @Names = SUBSTRING(@Names, @PropPos, LEN(@Names) - @PropPos)
SELECT @start = CAST(SUBSTRING(@Names, 1, CHARINDEX(':', @Names) - 1) AS tinyint) + 1
SELECT @length = CAST(SUBSTRING(@Names, CHARINDEX(':', @Names) + 1, ((CHARINDEX(':', @Names, CHARINDEX(':', @Names) + 1)) - (CHARINDEX(':', @Names) + 1))) AS tinyint)
SELECT @ret = SUBSTRING(@Values, @start, @length)

RETURN @ret

END

6/15/2009 7:58:02 AM | by Ash
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Thank you! This is exactly what I was looking for.

5/15/2010 7:56:01 AM | by Gabor Adam
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Brilliant. Thanks a lot. Atip C single function worked for me too.

5/25/2010 8:06:54 AM | by nrg
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Actually, Atip C single function didn't work for me on all occassions. I reverted back to the two and that's working fine.

5/26/2010 8:05:26 AM | by nrg
Gravatar

Re:How to get asp.net profile value from MS SQL database using T-SQL?

Well Atip C's solution works perfectly. Just the job. Remember to call as follows:

dbo.GetProperty(CustomerFeedback.CustomerID, 'LastName')

where 'LastName' is the property and is enclosed in quotes

7/15/2010 9:18:05 PM | by Phil

New Comment

Your Name:
Email (for internal use only):
Subject:
Comment:
 
Code above:

Categories

Recent Tweets

Valid XHTML 1.0 Transitional