Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to use SQL localdb for my application in C#.

C#
Data Source=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=|DataDirectory|\Files\test.mdf


It is running in mp pc, but when I try to run program in another new pc it gives exception. I installed only .Net 4.0 and SqlLocaLDB.MSI in new pc.

Do I need to install another thing? SQL Server Express completely?

Here's the stacktrace, thank you!

C#
System.Data.SqlClient.SqlException (0x80131904): 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)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
Posted

1 solution

It fails to connect your db.
what to do:
1- attach database file to your server before connecting it.
a. open ssms (sqlserver managament studio).
b. connect your localserver (localhost or . for default instance or localhost\<instance_name>)
c. Databases->Attach
2- then:

C#
string connStr = @"Data Source=.\MyInstance; Initial Catalog=MyDbName; Integrated Security=true;";
using(SqlConnection conn = new SqlConnection(connStr)) {
  conn.Open();
  // whatever you like
}


http://www.connectionstrings.com/sql-server/[^]
 
Share this answer
 
Comments
Emre Ataseven 25-Jan-14 15:33pm    
But I'm using SQL Express localdb, so ur connection string is wrong, no?
http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
Do I need to install ssms to other pc? Localdb does not use SQL Server Engine, right?
Vedat Ozan Oner 25-Jan-14 16:04pm    
got it. forget about my solution. have you ever tried absolute path for the database file? AttachDbFileName=c:\Files\test.mdf
Emre Ataseven 25-Jan-14 16:20pm    
Yes, I'm using absolute path and it works in Pc A, but it does not in Pc B (new-formatted pc that has .Net framework and SqlLocaLDB.MSI installed only)
Vedat Ozan Oner 25-Jan-14 17:21pm    
says that: "LocalDB is not a replacement for SQL Server Express -- it is an addition to SQL Server Express lineup. While LocalDB is meant for developers, the regular SQL Server Express will continue to exist as a free SQL Server edition, fully compatible with and easily upgradeable to higher SQL Server editions." in which I understand that you still need sql express installed.
Emre Ataseven 25-Jan-14 17:54pm    
Yep, I did and it worked, thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900