-1

i have problem with connection string after make new windows it no work it said

A network-related or instance-specific error occurred while establishing a connection to SQL Server

This is my connection string:

<add name="storemanagerEntities1"
     connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.;AttachDbFilename=F:\ENjaz\KKKK\Store-Manager\Store-Manager\storemanager.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;Initial Catalog=storemanager;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True&quot;"
     providerName="System.Data.EntityClient"/> 

Is there a way to make my connection work anywhere?

Rahul
  • 5,603
  • 6
  • 34
  • 57
beginer
  • 27
  • 6
  • 5
    you can edit your question, no need to comment it. And wht is the F:\ drive ? local or network drive ? If local, then you got your explanation, if network, I would suggest to use the network path instead of mapped drive letter as it can change accross different computers of your organization. – Laurent S. May 24 '13 at 11:46
  • local one - not network – beginer May 24 '13 at 11:49
  • What do you mean by anywhere? Is it anywhere on the planet as long as you have an internet connection or is it on your local network (like at home or at office)? – Stefan May 24 '13 at 11:52
  • 2
    Well, then it seems pretty obvious why you got this error. If you're trying to open a local file which is NOT on the computer, obviously it can't work... – Laurent S. May 24 '13 at 11:53
  • any where mean when i deliver the project to my customer he can open --and when copy my project in other pc its work i am trying to open database.mdf in my pc but when chose it the error occurred – beginer May 24 '13 at 11:55
  • SQL managements studio is the left ? – beginer May 24 '13 at 12:05

1 Answers1

1

Change this part of your connection string

AttachDbFilename=|DataDirectory|\Store-Manager\storemanager.mdf

DataDirectory is a substitution string that could be controlled from your program using

AppDomain.CurrentDomain.SetData("DataDirectory", ".....");

This should be done before any data access code is called.
Of course on the target PC you should be sure to have the relative part of the path

So let me give a practical example.

You set your connection string with this

....;AttachDbFilename=|DataDirectory|\storemanager.mdf;....

In the target PC, you deploy your database file in a subfolder called Store-Manager that you create as a subfolder of the CommonApplicationData folder
(on Win7 this is C:\programdata).
Then, inside your code, before any data access code, you write

string appPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(appPath, "Store-Manager");

Of course, on the target PC the Microsoft Sql Server Express (or not) should be already installed.

See this article on Microsoft forums or this question on this same site

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • then i put my path ? but when path is changed For exp its on F/sdas/asdasd when i put the project with db in other drive D/wefrwe/werwer it will work ? @Steve sry for you but i am a new student in C# classes – beginer May 24 '13 at 12:15
  • AppDomain.CurrentDomain.SetData("|DataDirectory|\", "Store-Manager\storemanager.mdf "); i make Like this ? – beginer May 24 '13 at 12:18
  • You put the MDF file in a relative (to your application) known directory and then call SetData("DataDirectory", ....) to the executable path of your application. I assume you have a winform or wpf application – Steve May 24 '13 at 12:23
  • windows form really thanq :) what about the error "A network-related or instance-specific error occurred while establishing a connection to SQL Server " when try to attach the database to the project ? – beginer May 24 '13 at 12:25
  • Is Sql Server Express installed on the target PC? – Steve May 24 '13 at 12:34
  • no - the sql who set up with Visual studio – beginer May 24 '13 at 12:55
  • and plz you have my connection string can give me full new connection string with AppDomain.CurrentDomain.SetData ? – beginer May 24 '13 at 12:57
  • No way then. You need to install at least Sql Server Express on the target machine. It's a free download from [here](http://www.microsoft.com/en-us/sqlserver/get-sql-server/try-it.aspx) – Steve May 24 '13 at 12:58
  • ok i am downloading :) – beginer May 24 '13 at 13:01
  • what about full connection string by your way ? – beginer May 24 '13 at 13:01