SSHH网络工作室

用GridView列出Profile存储的全部用户信息如公司名称等

相信很多人用Profile存储用户信息如公司名称等,但是在后台想用列表的方式列出来,却是很难,试试我的方法吧。

1. 在数据库里建一视图:view_Profile
SELECT dbo.aspnet_Users.ApplicationId, dbo.aspnet_Users.UserName,
       dbo.aspnet_Profile.PropertyNames, dbo.aspnet_Profile.PropertyValuesString,
       dbo.aspnet_Profile.PropertyValuesBinary
FROM dbo.aspnet_Profile INNER JOIN
       dbo.aspnet_Users ON dbo.aspnet_Profile.UserId = dbo.aspnet_Users.UserId

2.在数据库里建一存储过程:proc_Profile_GetProfiles
CREATE PROCEDURE dbo.proc_Profile_GetProfiles
     @ApplicationName       nvarchar(256)
AS
BEGIN
     DECLARE @ApplicationId uniqueidentifier
     SELECT   @ApplicationId = NULL
     SELECT   @ApplicationId = ApplicationId FROM dbo.aspnet_Applications WHERE LOWER(@ApplicationName) = LoweredApplicationName
     IF (@ApplicationId IS NULL)
         RETURN

     SELECT UserName, PropertyNames, PropertyValuesString, PropertyValuesBinary
     FROM          dbo.view_Profile
     WHERE ApplicationId = @ApplicationId
END

3. 建一个新类:QwpUserController
     public class QwpUserController
     {
         public static List<ProfileBase> GetUsers()
         {
             Type type = HttpContext.Current.Profile.GetType();
             Database db = DatabaseFactory.CreateDatabase();
             DbCommand dbCommand = db.GetStoredProcCommand("proc_Profile_GetProfiles");
             db.AddInParameter(dbCommand, "ApplicationName", DbType.String, ProfileManager.Provider.ApplicationName);
             SqlDataReader reader1 = (SqlDataReader)db.ExecuteReader(dbCommand);
             List<ProfileBase> list = null;
             if(reader1.HasRows)
                 list = new List<ProfileBase>();
             while (reader1.Read())
             {
                 string userName = null;
                 string[] textArray1 = null;
                 string text1 = null;
                 byte[] buffer1 = null;
                 userName = reader1.GetString(0);
                 textArray1 = reader1.GetString(1).Split(new char[] { ':' });
                 text1 = reader1.GetString(2);
                 int num1 = (int)reader1.GetBytes(3, (long)0, null, 0, 0);
                 buffer1 = new byte[num1];
                 reader1.GetBytes(3, (long)0, buffer1, 0, num1);
                 ProfileBase profile = (ProfileBase)type.GetConstructor(new Type[0]).Invoke(new object[0]);
                 profile.Initialize(userName, true);

                 ParseDataFromDB(textArray1, text1, buffer1, profile.PropertyValues);
                 list.Add(profile);
             }
             return list;
         }
         static void ParseDataFromDB(string[] names, string values, byte[] buf, SettingsPropertyValueCollection properties)
         {
             if (((names != null) && (values != null)) && ((buf != null) && (properties != null)))
             {
                 try
                 {
                     for (int num1 = 0; num1 < (names.Length / 4); num1++)
                     {
                         string text1 = names[num1 * 4];
                         SettingsPropertyValue value1 = properties[text1];
                         if (value1 != null)
                         {
                             int num2 = int.Parse(names[(num1 * 4) + 2], CultureInfo.InvariantCulture);
                             int num3 = int.Parse(names[(num1 * 4) + 3], CultureInfo.InvariantCulture);
                             if ((num3 == -1) && !value1.Property.PropertyType.IsValueType)
                             {
                                 value1.PropertyValue = null;
                                 value1.IsDirty = false;
                                 value1.Deserialized = true;
                             }
                             if (((names[(num1 * 4) + 1] == "S") && (num2 >= 0)) && ((num3 > 0) && (values.Length >= (num2 + num3))))
                             {
                                 value1.SerializedValue = values.Substring(num2, num3);
                             }
                             if (((names[(num1 * 4) + 1] == "B") && (num2 >= 0)) && ((num3 > 0) && (buf.Length >= (num2 + num3))))
                             {
                                 byte[] buffer1 = new byte[num3];
                                 Buffer.BlockCopy(buf, num2, buffer1, 0, num3);
                                 value1.SerializedValue = buffer1;
                             }
                         }
                     }
                 }
                 catch
                 {
                 }
             }
         }
     }

4. 使用这个类返回List<ProfileBase>
就有了。

posted on 2009-01-05 11:05  山虎  阅读(248)  评论(0编辑  收藏  举报

导航