简体   繁体   English

SQL Server Express连接字符串/ FluentNHibernate

[英]SQL Server Express connection string / FluentNHibernate

I'm having some issues with my connection string. 我的连接字符串有些问题。 I've been testing a lot of different strings and approaches and now I'm stuck. 我一直在测试很多不同的字符串和方法,现在我被卡住了。

private static void InitializeSessionFactory()
{
    _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
                          .ConnectionString(
 @"Server=Data Source=.\SQLEXPRESS;DataBase=carDB.mdf;User ID=sa;Password=xxxSecretxxx;")
                          .ShowSql()
            )
            .Mappings(m =>
                      m.FluentMappings
                          .AddFromAssemblyOf<Car>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, false))
            .BuildSessionFactory(); 
    }

(I know I shouldn't be using 'sa' at all - why I've done so is solely because of lack of management software so I had to use the only account there were) (我知道我根本不应该使用'sa' - 为什么我这样做只是因为缺乏管理软件所以我不得不使用唯一的帐户)

I am using a SQL Server Express database. 我正在使用SQL Server Express数据库。

The exception I get is this: 我得到的例外是:

An invalid or incomplete configuration was used while creating a SessionFactory. 创建SessionFactory时使用了无效或不完整的配置。
Check PotentialReasons collection, and InnerException for more detail. 检查PotentialReasons集合,以及InnerException以获取更多详细信息。

This is my inner exception 这是我的内心例外

A network-related or instance-specific error occurred while establishing a connection to SQL Server. 建立与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. 验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)

Does anyone know how I should approach this? 有谁知道我应该怎么做? I've spent hours trying to get this to work. 我花了好几个小时试图让它发挥作用。

In the connection string, you don't specify the database file but the database name . 在连接字符串中,不指定数据库文件,而是指定数据库名称 So it shouldn't be 所以它不应该

@"Server=Data Source=.\SQLEXPRESS;DataBase=carDB.mdf;User ID=sa;Password=xxxSecretxxx;"

but

@"Server=Data Source=.\SQLEXPRESS;DataBase=carDB;User ID=sa;Password=xxxSecretxxx;"

provided that the database really has the name carDB and not some other like Cars . 前提是数据库确实有carDB的名称,而不是像Cars这样的其他名称。

Also, I recommend using the SqlConnectionStringBuilder class to build connection strings, as it guarantees for syntactically correct connection strings. 另外,我建议使用SqlConnectionStringBuilder类来构建连接字符串,因为它保证了语法上正确的连接字符串。

On a side note: You do know that you can install the management tools (like SQL Server Management Studio for SQL Server Express) for the Express edition, too? 旁注:您确实知道可以为Express版安装管理工具(如SQL Server Management Studio for SQL Server Express)吗? This makes things much easier. 这使事情变得更容易。

It's not clear exactly what edition of SQL Server you're using. 目前尚不清楚您正在使用的SQL Server版本。 It's ether Express or Compact. 它是ether Express或Compact。

If it's SQL Express then try this: 如果它是SQL Express,那么试试这个:

@"Data Source=.\SQLEXPRESS;Initial Catalog=carDB;User ID=sa;Password=********;"

For SQL Compact usually enough to specify the path and the name of db file: 对于SQL Compact,通常足以指定db文件的路径和名称:

@"Data Source=path\\carDB.sdf;"

But for SQL Compact you probably need another provider MsSqlCeConfiguration.Standard 但对于SQL Compact,您可能需要另一个提供程序MsSqlCeConfiguration.Standard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM