3

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 :'(

6
  • 3
    Please don't quit programming. Commented May 19, 2013 at 7:43
  • If you're using the SQL Server database with the AttachDbFileName= approach on your client's system - does the client have SQL Server Express installed on his machine?? Commented May 19, 2013 at 9:14
  • @marc_s No, the Client don't have SQL Server express installed on his machine but he have SQL Compact installed!!! Commented May 19, 2013 at 9:17
  • 1
    If you're using an .mdf file and if you're using the AttachDbFileName= approach, you MUST have SQL Server Express installed on that machine. Compact won't do. Commented May 19, 2013 at 9:32
  • Check out Differences between SQL Server Express and Compact Edition for a list of how the two are different Commented May 19, 2013 at 9:35

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess you are right. I develop my application using SQL Server 2008 R2, but since it was not working on client's machine, i searched for the problem and came to a result that i have to just install SQLCE on client's machine to make my program run, without changing anything on my program. The connection string and the .mdf database are all of SQL Server type But the question is, what to do now? :(
It is a very tough situation for windows form application to install sql server to client. Is there any way to deploy and install to client pc without extra software installation headache ?
1

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! :)

Comments

0

"|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");

2 Comments

so, what should i do now? How to point out to the database in the root directory in my deployed application?
In the code example above just delete part + "DB" or create a folder DB and put your db file into the folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.