开发者

c# Connecting to a local MDF DB File

开发者 https://www.devze.com 2023-01-13 20:23 出处:网络
On my development computer I have MS SQL Server/Visual Studio 2005. My program can correctly connect to my local DB and use it. However my other computer (non-dev) does not have MS SQL Server/Visual S

On my development computer I have MS SQL Server/Visual Studio 2005. My program can correctly connect to my local DB and use it. However my other computer (non-dev) does not have MS SQL Server/Visual Studio 2005 and does not connect to the DB. It spits out the following:

"An error has occurrred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. ..." (Error: 26).

Does this mean I have to install SQL Server 2005 on my non-dev computers? Is there any other way?

My connection string is:

"Data Source=.\SQLEXPRESS;AttachDbFilename=\"" + Directory.GetCurrentD开发者_如何学Goirectory() + "\DB.mdf\";Integrated Security=True;User Instance=True";


Your connection string is telling the Sql Server Native Client ADO.NET Provider to attempt to connect to a Sql Server instance named SQLEXPRESS that will manage the database stored in a file DB.mdf. Since your client computer does not have Sql Server Express installed, it's not going to find a database to connect to.

You will need to:

  • Install Sql Server on the client computer and deploy your database there.
  • Switch to Sql Server Compact Edition (SqlCE - embedded database) and re-architect your application to use the portable database file (with SqlCE) instead.
  • Ditch using a robust database engine and switch entirely to ADO.NET DataSets, saving/loading the contents of the DataSet to an Xml file (via WriteXml() and ReadXml()). If the amount of data you are processing is fairly limited in size, DataSets are a good approach to maintaining integrity (via a strongly typed and well-defined schema) and portability.


If you want to run it on that other pc/server you also need to have SQL Server (need to attach your .mdf to it) or SQL Server Express installed.

Another trick would be to change your connection string so that it points out to another pc/server where your database runs on.


Yes, you do need MS-SQL (Express) available on your target computers. Either a local install or a connection to a server.

It is not that difficult to include SQl Express in a Setup.exe (see PreRequisites).

An alternative is to use SQL-CE or Sqlite or (even) MS-Access. They are 'embedded' database engines so that you only need to distribute DLLs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号