3

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.

2
  • Your SQL Server can be either Compact (single file with .sdf extension), or Express (at least two files - one .mdf, another .ldf) - but not both at the same time! So which is it?? Commented Jan 15, 2013 at 5:42
  • Thanks, then it's a Express.. will edit my question Commented Jan 15, 2013 at 9:41

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"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." - Yes, I've downloaded the tools and will install / configure my SQL Express as soon as i get home. Thanks
0

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

1 Comment

Thank you, I will try this when I get home! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.