1

I'm trying to create a blog with ASP.NET MVC. I'm following this guide

At the middle of the guide (page 7) we test the app, but we have to change the connection string.

Since I don't have a database set up, I was hopping the connection will create it automatically, but it is not.

<add name="BlogDbConnString" connectionString="Data Source=WATCHTOWER\SQLEXPRESS;Initial Catalog=Blog;Integrated Security=True" providerName="System.Data.SqlClient"/>

I also have my user set with no password:

<authentication mode="Forms">
  <forms loginUrl="~/Login" timeout="2880">
    <credentials passwordFormat="Clear">
      <user name="XXXXX" password=""/>
    </credentials>
  </forms>
</authentication>

When I try to run the app, I get an error in the code where the Ninject is supposed to create the DB:

public class RepositoryModule : NinjectModule
{
    public override void Load()
    {
        Bind<ISessionFactory>()
          .ToMethod(e => Fluently.Configure()
          .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("AngelAlferezBlogDbConnString")))
          .Cache(c => c.UseQueryCache().ProviderClass<HashtableCacheProvider>())
          .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Post>())
              .ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false))  //<===THIS LINE!
          .BuildConfiguration()
          .BuildSessionFactory())
          .InSingletonScope();

        Bind<ISession>()
          .ToMethod((ctx) => ctx.Kernel.Get<ISessionFactory>().OpenSession())
          .InRequestScope();
    }
}

And a Hibernate exception saying:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Anyone can guess what am I doing wrong?

4

1 回答 1

2

错误消息实际上说明了一切...... Hibernate 正在尝试创建数据库,但无法建立到 SQL Server 的连接来执行此操作。

这是一种非常常见的错误类型(通常与网络协议或安全性的配置有关)。

有关诊断和解决此类错误的良好清单,请参阅本文

http://blog.sqlauthority.com/2009/05/21/sql-server-fix-error-provider-named-pipes-provider-error-40-could-not-open-a-connection-to-sql-服务器-microsoft-sql-server-error/

文章中的步骤摘要:

  • SQL Server 应该已启动并正在运行。
  • 在 SQL Server 配置中启用 TCP/IP
  • 在 Windows 防火墙中打开端口
  • 启用远程连接
  • 启用 SQL Server 浏览器服务
  • 在防火墙中创建 sqlbrowser.exe 的异常
  • 重新创建别名
于 2013-10-09T15:50:16.267 回答