2008-8-5 15:39:06 喝小酒的网摘
 

asp.net中使用profile的错误


在Visual Web Developer 2008中,新建了网站项目,在网站项目中的Web.config中配置了

<anonymousIdentification enabled="true"/>
    <profile enabled="true">
      <properties>
        <add name="MyNewProperty" allowAnonymous="true"/>
      </properties>
    </profile>

在测试页中,使用如下:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            Profile.MyNewProperty = "Red";
        }

    }

奇怪的是运行后报错:
用户代码未处理 System.Web.HttpException
  Message="无法连接到 SQL Server 数据库。"
  Source="System.Web"
  ErrorCode=-2147467259
  StackTrace:
       在 System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString)
       在 System.Web.DataAccess.SqlConnectionHelper.EnsureSqlExpressDBFile(String connectionString)
       在 System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation)
       在 System.Web.Profile.SqlProfileProvider.GetPropertyValuesFromDatabase(String userName, SettingsPropertyValueCollection svc)
       在 System.Web.Profile.SqlProfileProvider.GetPropertyValues(SettingsContext sc, SettingsPropertyCollection properties)
       在 System.Configuration.SettingsBase.GetPropertiesFromProvider(SettingsProvider provider)
       在 System.Configuration.SettingsBase.SetPropertyValueByName(String propertyName, Object propertyValue)
       在 System.Configuration.SettingsBase.set_Item(String propertyName, Object value)
       在 System.Web.Profile.ProfileBase.SetInternal(String propertyName, Object value)
       在 System.Web.Profile.ProfileBase.set_Item(String propertyName, Object value)
       在 System.Web.Profile.ProfileBase.SetPropertyValue(String propertyName, Object propertyValue)
       在 ProfileCommon.set_MyNewProperty(String value) 位置 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\lesson 4\cd4dc028\df5baf87\App_Code.x9_y_xdn.0.cs:行号 24
       在 _Default.Page_Load(Object sender, EventArgs e) 位置 c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\WebSites\Lesson 4\Default.aspx.cs:行号 21
       在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       在 System.Web.UI.Control.OnLoad(EventArgs e)
       在 System.Web.UI.Control.LoadRecursive()
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: System.Web.HttpException
       Message="无法连接到 SQL Server 数据库。"
       Source="System.Web"
       ErrorCode=-2147467259
       StackTrace:
            在 System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString)
            在 System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install)
            在 System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString)
            在 System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString)
       InnerException: System.Data.SqlClient.SqlException
            Message="在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)"
            Source=".Net SqlClient Data Provider"
            ErrorCode=-2146232060
            Class=20
            LineNumber=0
            Number=-1
            Server=""
            State=0
            StackTrace:
                 在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
                 在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
                 在 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
                 在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
                 在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
                 在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
                 在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
                 在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
                 在 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
                 在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
                 在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
                 在 System.Data.SqlClient.SqlConnection.Open()
                 在 System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString)
            InnerException:

在网上看到的说法是"一般profile会有一个自己的数据库 ",即profile中的数据还是用数据库来保存的,不知道我的是没有这个数据库,还是什么原因,连接不上.