修改ProfileBase的ConnectionString动态 [英] Modifying ProfileBase ConnectionString dynamically

查看:223
本文介绍了修改ProfileBase的ConnectionString动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,我想用修改 ProfileBase 的connectionString:

  ProfileBase配置= ProfileBase.Create(用户名);字符串_connectionString =(_DataModel.Connection为System.Data.EntityClient.EntityConnection).StoreConnection.ConnectionString;字段信息connectionStringField = profile.Providers [MySqlProfileProvider]的GetType()BaseType.GetField(_ sqlConnectionString,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)。
connectionStringField.SetValue(profile.Providers [MySqlProfileProvider],_connectionString);简介[名字] =名字;
简介[姓] =姓;profile.Save();

首先所有的 connectionStringField 总是回来为空,但是我可以看到 profile.Providers 确实包含 MySqlProfileProvider 。这是我的的Web.Config中指定

 <型材defaultProvider =MySqlProfileProvider>
  <供应商>
    <清/>
    <添加名称=MySqlProfileProvider的connectionStringName =MyApp的的applicationName =MyApp的TYPE =System.Web.Profile.SqlProfileProvider,System.Web程序,版本= 2.0.0.0,文化=中性公钥= b03f5f7f11d50a3a/>
  < /供应商>
  <性状>
    <添加使用AllowAnonymous =false的设置defaultValue =NAME =姓readOnly的=假serializeAs =字符串类型=System.String/>
    <添加使用AllowAnonymous =false的设置defaultValue =NAME =姓readOnly的=假serializeAs =字符串类型=System.String/>
  < /性状>
< / profile文件>

我的问题是怎么来的 connectionStringField 是回来为空?这是否意味着我不能修改连接字符串,就像我通常会用一个自定义的的MembershipProvider 通过覆盖其初始化方法<? / p>

解决方案

您去太多了基本类型下来:

  .Providers [MySqlProfileProvider]。的GetType()**。**基本类型。getfield命令
.Providers [MySqlProfileProvider]。的GetType()。getfield命令

以下code应该工作:

 字符串_connectionString =(_DataModel.Connection为System.Data.EntityClient.EntityConnection).StoreConnection.ConnectionString;
类型type = profile.Providers [MySqlProfileProvider]的GetType()。
字段信息connectionStringField = type.GetField(_ sqlConnectionString,System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
connectionStringField.SetValue(profile.Providers [MySqlProfileProvider],_connectionString);

I have the following code, which I want to use to modify the ProfileBase connectionString:

ProfileBase profile = ProfileBase.Create(username);

string _connectionString = (_DataModel.Connection as System.Data.EntityClient.EntityConnection).StoreConnection.ConnectionString;

FieldInfo connectionStringField = profile.Providers["MySqlProfileProvider"].GetType().BaseType.GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
connectionStringField.SetValue(profile.Providers["MySqlProfileProvider"], _connectionString);

profile["FirstName"] = firstName;
profile["Surname"] = surname;

profile.Save();

First of all the connectionStringField always comes back as null, however I can see that profile.Providers does contain MySqlProfileProvider. This is specified within my Web.Config:

<profile defaultProvider="MySqlProfileProvider">
  <providers>
    <clear/>
    <add name="MySqlProfileProvider" connectionStringName="MyApp" applicationName="MyApp" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </providers>
  <properties>
    <add allowAnonymous="false" defaultValue="" name="FirstName" readOnly="false" serializeAs="String" type="System.String"/>
    <add allowAnonymous="false" defaultValue="" name="Surname" readOnly="false" serializeAs="String" type="System.String"/>
  </properties>
</profile>

My question is how come connectionStringField is coming back as null? Does this mean I cannot modify the connection string like I normally would with a custom MembershipProvider by overriding its Initialize method?

解决方案

You went one too many basetypes down:

.Providers["MySqlProfileProvider"].GetType()**.BaseType**.GetField
.Providers["MySqlProfileProvider"].GetType().GetField

The following code should work:

string _connectionString = (_DataModel.Connection as System.Data.EntityClient.EntityConnection).StoreConnection.ConnectionString;
Type type = profile.Providers["MySqlProfileProvider"].GetType();
FieldInfo connectionStringField = type.GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
connectionStringField.SetValue(profile.Providers["MySqlProfileProvider"], _connectionString);

这篇关于修改ProfileBase的ConnectionString动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆