我正在尝试使用C#通过NHibernate连接到SQL Server 2008服务器实例。我无法这样做。
我已成功使用java通过Hibernate连接到同一个实例。所以我确信我的SQL Server 2008配置设置没有任何问题。我还设法连接到SQL Server 2008 Express。
这是hibernate.cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">server=(local)\SUNIL,3067;Initial Catalog=NHibernate101;User ID=sunil;Password=mypassword;</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="cache.use_query_cache">false</property>
<property name="adonet.batch_size">100</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Infrastructure" />
</session-factory>
</hibernate-configuration>
以下是错误消息:
测试方法 NHibernate101.Tests.RepositoriesTest.CanCreatePost抛出异常: System.Data.SqlClient.SqlException: 用户'sunil'登录失败。
这是堆栈跟踪:
System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION exception,Boolean breakConnection) System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj) System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(布尔 enlistOK) System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer超时,SqlConnection owningObject) System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer超时) System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(的SqlConnection owningObject,TimeoutTimer超时, 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池,DbConnection owningConnection) System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(的DbConnection owningConnection,DbConnectionPool pool,DbConnectionOptions选项) System.Data.ProviderBase.DbConnectionPool.CreateObject(的DbConnection owningObject) System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(的DbConnection owningObject) System.Data.ProviderBase.DbConnectionPool.GetConnection(的DbConnection owningObject) System.Data.ProviderBase.DbConnectionFactory.GetConnection(的DbConnection owningConnection) System.Data.ProviderBase.DbConnectionClosed.OpenConnection(的DbConnection outerConnection,DbConnectionFactory 的connectionFactory) System.Data.SqlClient.SqlConnection.Open() NHibernate.Connection.DriverConnectionProvider.GetConnection() NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare() NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(方言 方言,IConnectionHelper connectionHelper) NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactory SessionFactory的) NHibernate.Impl.SessionFactoryImpl..ctor(配置 cfg,IMapping mapping,Settings 设置,EventListeners侦听器) NHibernate.Cfg.Configuration.BuildSessionFactory() Core.Domain.Repositories.NHibernateHelper.get_SessionFactory() 在 d:\ DOTNET \教程\ NHibernate101 \核心\域\库\ NHibernateHelper.cs: 第22行 Core.Domain.Repositories.NHibernateHelper.OpenSession() 在 d:\ DOTNET \教程\ NHibernate101 \核心\域\库\ NHibernateHelper.cs: 第30行 Core.Domain.Repositories.CategoryRepository.Core.IRepository.Save(范畴 实体) d:\ DOTNET \教程\ NHibernate101 \核心\域\库\ CategoryRepository.cs: 第17行 NHibernate101.Tests.RepositoriesTest.CanCreatePost() 在 d:\ DOTNET \ NHibernate101 \ NHibernate101.Tests \ RepositoriesTest.cs: 第69行
答案 0 :(得分:1)
错误信息似乎非常明显:
System.Data.SqlClient.SqlException: 用户“sunil”登录失败。
这表示用户在连接字符串中指定了
因此,我会尝试修复您的连接字符串,以确保您可以登录:
server=(local)\SUNIL,3067;Initial Catalog=NHibernate101;
User ID=sunil;Password=mypassword;
SUNIL
的SQL Server实例并驻留在本地计算机上吗?NHibernate101
?sunil
的登录?mypassword
?sunil
中是否存在NHibernate101
的用户?您是否可以从SQL Server Management Studio连接到那个服务器,那个数据库,具有那个登录名和密码?