Reputation: 1198
I have developed a Windows Form Application in C# that uses SQL Server database connectivity. I developed it in .NET Framework 4.0 and SQL Server 2008 R2. My application is using a local database present in the root directory of the application called AG.mdf
.
This is my connection string:
Data Source=.;AttachDbFilename=|DataDirectory|\\AG.mdf;Integrated Security=True;User Instance=True
I created the installer by adding Setup Project inside Visual Studio installer template as a new project in my application. I build the Setup project and it created the installer for me. I deployed the application on my User's system, the application starts initially but then it suddenly pops up an error
Unhandled exception has occurred in your application. If you click continue, the application will ignore this message and attempt to continue. If you click quit, the application will close immediately.
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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
P.S: I have installed .NET Framework 4.0 and SQL Server Compact Edition on my User's system as well!
Should I quit programming :'(
Upvotes: 3
Views: 6604
Reputation: 1198
Thanks a lot everyone for your help. I have solved this problem on my own. My only concern was about installing the SQL Server on client's machine but later i realized that i have to install a Database management tool (SQL Server) on client's system in order to make my application run.
Hence, I installed SQL Server Express on client's machine and change my QueryString a bit, as follows
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\AG.mdf;Database=AG;Integrated Security=True;User Instance=True;Trusted_Connection=Yes
Moreover, i also gave the Full Control Permission as Allow on that folder which contains my deployed application, so that Dot Net Framework in my application can access the deployed .mdf database
Happy coding! :)
Upvotes: 1
Reputation: 24954
"|DataDirectory|\AG.mdf" in means that it need to look DB in the Data Directory, but not in the root folder of the application.
Small example how to set the DataDirectory:
AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory + "DB");
Upvotes: 0
Reputation:
I think you might be getting Express Edition and Compact Edition confused; they're really quite different.
Compact Edition takes an SDF database file, and is an embedded database, not a separate server. I don't think it can work with MDF directly at all - you might be able to export an MDF to an SDF though.
SQLCE doesn't know about User Instances, Integrated Security, or database attachments. I think you'd need the path to the SDF file directly in the Data Source section, but it would need to be an SDF, not an MDF.
So basically, it looks like you're installing Compact Edition, but then using connect strings and data files for the full/express edition, which isn't likely to work.
Upvotes: 2