1

I am very new to SQL and tried to build a software by using SQLServer and Visual Studio. I created a setup file for my program by using InstallShield Limited Edition Project. I want my program to be used on other computers and I want every user to be able use their own databases installed on their computers. To me, when a user installed the program, the program will search for a connection string that I used while creating the program. Therefore, I think this connection string must be changed by users. How can I add such properties into my program? By the way, I used model first entity framework in my program. My connection string written in app.config is :

<connectionStrings>
    <add name="otobusVTNesneleri" connectionString="metadata=res://*/OtobusVeriModeli.csdl|res://*/OtobusVeriModeli.ssdl|res://*/OtobusVeriModeli.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=PIPASO\PIPASOSERVER;initial catalog=otobus;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

P.S. I searched the internet about this problem and saw that people suggest that I use SQL Server compact. I want to solve this problem without using it. I have SQL Server 2012.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • you can save the Connection in a file and read it from there whene you want and after the instalation of an app the user can go to an option dialog that you created where he can change the connection string and save it into the file – Youness Sep 05 '14 at 08:40
  • See [InstallShield XML File Changes - Change node attribute on install](http://stackoverflow.com/questions/16428449/installshield-xml-file-changes-change-node-attribute-on-install) to change the app.config's connectionstring. – CodeCaster Sep 05 '14 at 08:41
  • 1
    @Youness Thanks for the quick response. As I said, I am very new to C# and SQL. Can you be more specific please? e.g. what do you mean by saving the connection in a file? And what is an option dialog ? :( – Erdinc Karakullukcu Sep 05 '14 at 08:46
  • option dialog is just a form that a user can type and choose options you have to create it and i llbe help full if you want i will post how to write and read form a file – Youness Sep 05 '14 at 08:47
  • @CodeCaster I guess I have nothing to do with XML file. My program is not related to Web. – Erdinc Karakullukcu Sep 05 '14 at 08:48
  • XML and web have no relation whatsoever. Your applcation configuration file contains XML, which you apparently want to edit. If you don't understand this, start with something simpler. Is your question: _"How can I let an application access the database at the local machine after installation?"_? Then change the connection string to `localhost`. – CodeCaster Sep 05 '14 at 08:50

5 Answers5

1

You shouldinstall the SQL Server with the same instance name in all the PCs. And then, in the connection string, instead of specifying the computer name, use the "local" syntax:

Data Source=(local)\yourInstanceName

It's not a good idea to let your users change the connection string.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • OK. This seems reseonable, but how will the procedure work? User will install the program into his computer. Then, while connecting to server, he will write (local)\myInstanceName ? In addition, I guess I must add a warning to the program that tells the user to use myIstanceName. Did I understand it correctly? – Erdinc Karakullukcu Sep 05 '14 at 12:21
  • Nope! If you use the (local), and always install the same SQL Server instance name, the connection string will be exactly the same for all the installations. So, the user doesn't have to change anything. I.e. the setup will create the same .config file for all the installations, and the user will do nothing. That's way using (local) is advantageous – JotaBe Sep 05 '14 at 13:04
  • I am sorry that I am new to SQL. What do you mean by saying "install the same SQL Server instance name"? In order to be able to use the database I guess users must connect to a server in their computers. Therefore, they will need an SQL Server instance name and shouldn't I provide this information to the users? – Erdinc Karakullukcu Sep 05 '14 at 13:45
  • When you install SQL Server in a computer, unless you change it, the "default instance is installed". That means that you access the SQL Server using the server name, or local. If you want to install other SQL Server, you have to install it as "named instance", so that the user can access it without conflict. In this case, to access the server you need to specify "server name\instance name" (or "(local)\instace name"). This is done to allow having several different SQL Servers running side by side. Once you know it, tweak your setup to install SQL Server as named instance. – JotaBe Sep 06 '14 at 01:56
1

Entity Framework starting from version 6 supports Code Based configuration. Refer to this article: Code-Based Configuration (EF6 onwards)

Dmitri Trofimov
  • 574
  • 2
  • 17
0

local databases should be sql compact (sdf files). in any case, what you need to do is to make the installer modify the app.config file after it's deployed. this is what i'm doing in my current project. in my current project i have a local database (an sdf file) and an installer (created using WiX) that deploys my app. i configured the installer to modify some app.config settings after deploying the app. one of the settings was the connection to the local sdf database.

one thing to be careful about is that, while using sdf files, you cannot specify relative file paths in the config file. you need to either specify the full path (ie: d:\..\db.sdf) or use special folder names (ie |AppData|db.sdf). AppData will resolve to the current path of the executable.

requiring a full blown sql database on the client will also mean you'll have to install SQL Express on the client machine. you don't have to do this with sdf files.

in case you decide to use WiX you can modify your connection string like below.

<util:XmlFile Id="WindowsServiceUpdateConnectionString" File="[INSTALLFOLDER]$(var.Phoenix.WindowsService.TargetFileName).config" Action="setValue"
ElementPath= "//configuration/connectionStrings/add[\[]@name=&quot;PhoenixCacheEntities&quot;[\]]/@connectionString"
Value="Data Source=[INSTALLFOLDER]Cache\PhoenixLocal.sdf"/>

you can see that an xpath is used here to determine the setting you want to change. hope this helps.

flo_badea
  • 774
  • 5
  • 8
0

without use sql server username password use it.

<configuration>
  <connectionStrings>
     <add name="ConString" connectionString="Data Source=Trainee4-PC;Initial Catalog=Demo05-09-2014;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

use sql server username password use it.

<configuration>
  <connectionStrings>
     <add name="ConString" connectionString="Data Source=DENISH\SA;Initial Catalog=Demo05-09-2014;User ID=Demo;Password=Demo123" />
  </connectionStrings>
</configuration>
Genish Parvadia
  • 1,437
  • 3
  • 17
  • 30
-1

NOT RECOMANDED

its just becouse you said you are just starting in c#

so to write the connection string into a file :

using System.IO;//delcare it the top
File.Write("config.cfg","your connection string goes here");

to read From it :

using System.IO;//delcare it the top
string CNXSTRING = File.Read("config.cfg");

btw you have to create a form where the user and can type the new connection string lets say it have a textbox named txtstring the code will become

File.Write("config.cfg",txtstring.Text);

PS : use the write when you want to save the CNX String like in the save button of that form .

Youness
  • 1,468
  • 1
  • 9
  • 19