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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sohaiby
  • 1,168
  • 3
  • 24
  • 39
  • 3
    Please don't quit programming. – rptwsthi May 19 '13 at 07: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?? – marc_s May 19 '13 at 09:14
  • @marc_s No, the Client don't have SQL Server express installed on his machine but he have SQL Compact installed!!! – sohaiby May 19 '13 at 09: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. – marc_s May 19 '13 at 09:32
  • Check out [Differences between SQL Server Express and Compact Edition](http://blog.sqlauthority.com/2009/04/22/sql-server-difference-between-sql-server-compact-edition-ce-and-sql-server-express-edition/) for a list of how the two are different – marc_s May 19 '13 at 09:35
  • @marc_s Thanks alot. Can u please guide me that what changes should i do to make my application run on user's machine? I don't want to install the heavy software of SQL Server on user's machine. What changes do my application needs to use SQLCE database? – sohaiby May 19 '13 at 10:33

3 Answers3

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.

Community
  • 1
  • 1
  • 1
    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? :( – sohaiby May 19 '13 at 09:21
  • 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 ? – Kabir Hossain May 14 '18 at 09:58
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! :)

sohaiby
  • 1,168
  • 3
  • 24
  • 39
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");
Roman Podlinov
  • 23,806
  • 7
  • 41
  • 60