0

I have problem to connect my application (on computer A) to another computer (computer B) that had the database (.mdf file).

This is my code

<connectionStrings>
   <add name="HS_App.My.MySettings.DatabaseHSConnectionString" 
        connectionString="Data Source=Computer-B\SQLEXPRESS;AttachDbFilename=Computer-B\Users\Computer-B\Desktop\HS App\DBHS\DatabaseHS.mdf;Integrated Security=True;User Instance=True"
        providerName="System.Data.SqlClient" />
</connectionStrings>

Computer A and B are connected via adhoc, and have the same environment ip address. Can I do that? Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nore
  • 111
  • 2
  • 4
  • 20
  • Remote connections (from another computer) to a SQL Server Express database are disabled by default; see [this other SO question](http://stackoverflow.com/questions/11278114/enable-remote-connections-for-sql-server-express-2012) and its answers on how to **enable** remote connection for a SQL Server **Express** instance – marc_s Jun 08 '13 at 10:38

1 Answers1

0

If you want to use a local SQL Server Express instance using AttachDbFileName for a database located on another computer it will not work: SQL Server requires local filesystem access because it does some interesting under-the-hood filesystem stuff with the database files (.mdf and the log files), so MDF files located on a network share (UNC path) or Internet location will not work, see here: http://msdn.microsoft.com/en-us/library/ms254500(v=vs.80).aspx

System.Data.SqlClient resolves the substitution strings into full paths against the local computer file system. Therefore, remote server, HTTP, and UNC path names are not supported. An exception is thrown when the connection is opened if the server is not located on the local computer.

If you want to connect to an already-running SQL Server Express instance on another computer over TCP, then you need to set-up TCP connections on that server first. Note that you cannot use AttachDbFilename either because that option only applies to "user instances" of SQL Server Express.

Dai
  • 141,631
  • 28
  • 261
  • 374