如何更改profilebase的默认连接字符串 [英] how to change default connection string of profilebase
问题描述
我在我的网络应用程序中使用配置文件功能。我在使用ASP.NET配置文件功能获得了一篇有用的文章在Web应用程序项目中 [ ^ ]。根据这篇文章,我编写了我的`web.config`,一个自定义的配置文件类,一个配置文件信息类。以下是我的代码。
我的代码
web.config
I''m using the Profile Feature in my web application. I got a helpful article at Using ASP.NET Profile Feature in a Web Application Project[^]. According to this article I coded my `web.config` , a custom profile class, a profile info class. Following is my code.
My Code
web.config
<profile enabled="true" inherits="app_DeliverMatic.UserProfile" defaultProvider="ProfileProvider">
<providers >
<add name="ProfileProvider" applicationName="delivermatic" connectionStringName="LocalSqlServer" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</profile>
<connectionStrings>
<clear/>
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=mydb;Persist Security Info=True;User ID=uid;Password=pass;"/>
</connectionStrings>
UserProfile.vb
UserProfile.vb
Public Class UserProfile
Inherits ProfileBase
Public ReadOnly Property UserProfileInfo As UserProfileInfo
Get
Return DirectCast(GetPropertyValue("UserProfileInfo"), UserProfileInfo)
End Get
End Property
Public Shared Function GetProfile() As UserProfile
Return DirectCast(HttpContext.Current.Profile, UserProfile)
End Function
Public Shared Function GetProfile(ByVal UserName As String) As UserProfile
Return DirectCast(Create(UserName), UserProfile)
End Function
End Class
UserProfileInfo.vb
UserProfileInfo.vb
<Serializable()>
Public Class UserProfileInfo
Dim _Name As String
Dim _Company As String
Dim _Address1 As String
Dim _Address2 As String
Dim _City As String
Dim _State As String
Dim _Country As String
Dim _Zip As String
Dim _PhoneNo As String
Dim _Mobile As String
Dim _FaxNo As String
Dim _Website As String
Dim _Registration_Email As String
Public Property Name As String
Set(ByVal value As String)
_Name = value
End Set
Get
Return _Name
End Get
End Property
Public Property Company As String
Set(ByVal value As String)
_Company = value
End Set
Get
Return _Company
End Get
End Property
Public Property Address1 As String
Set(ByVal value As String)
_Address1 = value
End Set
Get
Return _Address1
End Get
End Property
Public Property Address2 As String
Set(ByVal value As String)
_Address2 = value
End Set
Get
Return _Address2
End Get
End Property
Public Property City As String
Set(ByVal value As String)
_City = value
End Set
Get
Return _City
End Get
End Property
Public Property State As String
Set(ByVal value As String)
_State = value
End Set
Get
Return _State
End Get
End Property
Public Property Country As String
Set(ByVal value As String)
_Country = value
End Set
Get
Return _Country
End Get
End Property
Public Property Zip As String
Set(ByVal value As String)
_Zip = value
End Set
Get
Return _Zip
End Get
End Property
Public Property PhoneNo As String
Set(ByVal value As String)
_PhoneNo = value
End Set
Get
Return _PhoneNo
End Get
End Property
Public Property Mobile As String
Set(ByVal value As String)
_Mobile = value
End Set
Get
Return _Mobile
End Get
End Property
Public Property FaxNo As String
Set(ByVal value As String)
_FaxNo = value
End Set
Get
Return _FaxNo
End Get
End Property
Public Property Website As String
Set(ByVal value As String)
_Website = value
End Set
Get
Return _Website
End Get
End Property
Public Property Registration_Email As String
Set(ByVal value As String)
_Registration_Email = value
End Set
Get
Return _Registration_Email
End Get
End Property
Public Sub New()
End Sub
Public Sub New(ByVal Name As String, ByVal Company As String, ByVal Address1 As String, ByVal Address2 As String, ByVal City As String, ByVal State As String, ByVal Country As String, ByVal Zip As String, ByVal PhoneNo As String, ByVal Mobile As String, ByVal FaxNo As String, ByVal Website As String, ByVal Registration_Email As String)
Me.Name = Name
Me.Company = Company
Me.Address1 = Address1
Me.Address2 = Address2
Me.City = City
Me.State = State
Me.Country = Country
Me.Zip = Zip
Me.PhoneNo = PhoneNo
Me.FaxNo = FaxNo
Me.Website = Website
Me.Registration_Email = Registration_Email
End Sub
End Class
使用配置文件提供程序的代码
Code To Use The Profile Provider
Dim profile As UserProfile = UserProfile.GetProfile(lblEmail.Text)
With profile.UserProfileInfo
.Address1 = txtAddress1.Text
.Address2 = txtAddress2.Text
.City = txtCity.Text
profile.Save()
End With
**我的问题**
上面的代码工作得很好。但问题是,如果我将连接字符串名称设置为我想要的任何内容,则会出错。更改连接字符串名称时,我还将配置文件提供程序的connectionStringName属性设置为新名称。似乎ProfileBase类仅适用于设置为LocalSqlServer的connectionStringName。我怎样摆脱这个问题?
**My Problem**
The above code works just fine. But the problem is, if I set the connection string name to anything I want, it gives error. When changing the connection string name I also setting the "connectionStringName" attribute of the profile provider to the new name. It seems that the ProfileBase class works only with connectionStringName set to LocalSqlServer. How can I get rid of this problem?
推荐答案
你的错误意味着它仍然在某个地方寻找那个名字。您是否搜索过整个项目以确保名称在任何地方都不存在?它必须来自某个地方
Your error means it''s still looking for that name somewhere. Have you searched your whole project to make sure that name does not exist anywhere ? It has to be coming from somewhere
解决它。添加了< clear />在添加配置文件提供程序之前。
Solved it. Added "<clear/>" just before adding the profile provider.
<providers>
<clear />
<add name="ProfileProvider" applicationname="delivermatic" connectionstringname="myCon" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
这篇关于如何更改profilebase的默认连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!