简体   繁体   中英

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)

I am using a SQL Server Express database.

The exception I get is this:

An invalid or incomplete configuration was used while creating a SessionFactory.
Check PotentialReasons collection, and InnerException for more detail.

This is my inner exception

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)

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 .

Also, I recommend using the SqlConnectionStringBuilder class to build connection strings, as it guarantees for syntactically correct connection strings.

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? This makes things much easier.

It's not clear exactly what edition of SQL Server you're using. It's ether Express or Compact.

If it's SQL Express then try this:

@"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:

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

But for SQL Compact you probably need another provider MsSqlCeConfiguration.Standard

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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