特殊需求
一個非常特別需求,需要提取aspnet_Profile中自訂的使用者屬性。這個特殊的需求是這樣的:
提取使用者的屬性列表,我的機器只能訪問開發庫,沒法訪問生產庫。DBA也比較忙,於是乎就把未經處理資料拿了過來,用程式進行分解。
ProfileObject Storage Service表(aspnet_Profile)
欄位名 |
類型 |
屬性 |
說明 |
UserId |
uniqueidentifier |
FK: aspnet_Users.UserId |
使用者ID |
PropertyNames |
ntext |
|
屬性名稱 |
PropertyValuesString |
ntext |
|
字串值 |
PropertyValuesBinary |
image |
|
二進位值 |
資料是這個樣子的:
propertynames |
NID:S:0:3:Company:S:3:3:PID:S:6:3:TrueName:S:9:2: |
propertyvaluesstring |
338com549李某 |
C#實現 /// <summary>
/// 擷取 aspnet_Profile中的屬性值
/// </summary>
/// <returns></returns>
private DataTable GetProfileProperty()
{
string sql = @"SELECT
[username]
, [rolename]
, [propertynames]
, [propertyvaluesstring]
FROM [VUserProfile]";
DataSet ds = DBUtility.getDataSet(sql);
//NID,Company,PID,TrueName是自訂的使用者屬性
DataTable dt = new DataTable();
DataColumn UserName = new DataColumn("UserName", typeof(string));
DataColumn RoleName = new DataColumn("RoleName", typeof(string));
DataColumn NID = new DataColumn("NID", typeof(string));
DataColumn Company = new DataColumn("Company", typeof(string));
DataColumn PID = new DataColumn("PID", typeof(string));
DataColumn TrueName = new DataColumn("TrueName", typeof(string));
dt.Columns.Add(UserName);
dt.Columns.Add(RoleName);
dt.Columns.Add(NID);
dt.Columns.Add(Company);
dt.Columns.Add(PID);
dt.Columns.Add(TrueName);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow row = ds.Tables[0].Rows[i];
string username = row["username"].ToString();
string rolename = row["rolename"].ToString();
string propertynames = row["propertynames"].ToString().Replace(":S:", "*");
string propertyvaluesstring = row["propertyvaluesstring"].ToString();
DataRow dtrow = dt.NewRow();
dtrow["UserName"] = username;
dtrow["RoleName"] = rolename;
string[] p1 = propertynames.Split('*');
for (int j = 0; j < p1.Length - 1; j++)
{
string[] tmp = p1[j].Split(':');
string[] tmp2 = p1[j + 1].Split(':');
string propertyname = tmp[tmp.Length - 1];
string propertyvalue = propertyvaluesstring.Substring(int.Parse(tmp2[0]), int.Parse(tmp2[1]));
dtrow[propertyname] = propertyvalue;
}
dt.Rows.Add(dtrow);
}
return dt;
}
效果
UserName |
RoleName |
NID |
CompanyID |
PID |
TrueName |
leex |
客服 |
800 |
c1 |
201 |
李 X |
leey |
財務 |
801 |
c2 |
202 |
李 Y |
leez |
代理商 |
801 |
c3 |
203 |
李 Z |