12

I have created a C# program with a SQL Server database. It works fine on my computer but on my friend's PC it doesn't (my friend doesn't have SQL Sever 2008). Is it possible to make it without any installation? And if it can, how can it be done?

This is my connection string:

connectionString="Data Source=\v11.0;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;Integrated Security=True"
Sam
  • 7,252
  • 16
  • 46
  • 65
Petar Bechev
  • 587
  • 2
  • 5
  • 11
  • 3
    Possible duplicate of [LocalDB deployment on client PC](http://stackoverflow.com/questions/9655362/localdb-deployment-on-client-pc) – Wouter de Kort Jan 29 '16 at 08:04
  • Do you want your friend to have access to YOUR database or is the database unique? If the latter, note that a SQL Server is required for any local hosting requiring SQL. Use alternatives such as SQLite (for user input information) or Resource Files (for internal items) instead. If the former, you have to host a server yourself. – aeee98 Jan 29 '16 at 08:24

7 Answers7

11

SQL Server is for server databases. You can change your project to use SQL Server CE (SQL Server Compact Edition) which is a single-file local database. It is very similar to the "true" SQL Server so it may be the easiest solution. Your code probably wont change except for the connection string.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mike Tsayper
  • 1,686
  • 1
  • 17
  • 25
  • Petar Bechev, there is an extension for Visual Studio (SQL Server Compact Toolbox) you can install from Tools menu. Create a DB there, add it to your project. Then recreate your tables there and change your app's connection string to use this new DB. – Mike Tsayper Jan 29 '16 at 08:18
  • Isn't SQL CE deprecated/discontinued? – edmz Jan 29 '16 at 15:54
  • @black yes but its still doing its job. For a new project its better to go with SQLite as a local DB i think. – Mike Tsayper Jan 29 '16 at 16:09
5

Use the IP address of your local machine in which the database is present; 1433 is the default port number. Then modify the connection string accordingly:

connectionString="Data Source=190.190.200.100,1433;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"
edmz
  • 8,220
  • 2
  • 26
  • 45
Sanu Antony
  • 364
  • 4
  • 15
4

First time you open port 1433 in firewall if you don't need you can turn off firewall. go to Run=>cmd=>Ipconfig find network's card activity. Show like enter image description here

Like my computer have just wireless and my andress in private lan is:192.168.100.165 And now you change connectstring in web.config.

connectionString="Data Source=192.168.100.165;
Initial Catalog=yourDataBase;User ID=yourUsername;Password=yourPassword;"

If you don't know "yourUsername" and "yourpassword" please refer link and create username and password in MsSQL https://msdn.microsoft.com/en-us/library/aa337562.aspx

And if you want to connect database from internet you need to do open port your's router

Cuong Ngo
  • 76
  • 3
2

If you want to run your program without the SQL server installed you have to use a service based sql database.see this image

you can add a local database through Visual Studio (project--> Add new item)

2

My solution will cater if you need a SQL database on your friend's computer instead of hosting from your own.

The cheapest way to handle a database locally on any device using a proper database is to convert to SQLite. This is a local device alternative that is more lightweight and doesn't require any user to install a SQL server at all.

There are also alternatives like writing information to a hidden file (usually in binary if you don't want your application to be hacked).

In short, only consider a SQL database IF you are hosting the database, otherwise use alternatives.

aeee98
  • 114
  • 10
1

As far as I know, you have one of 2 options. You either have a server (a pc set up to be a server and containing this database) or you could take the easier option and host your database on the cloud. Many websites offer a free service to host your database for a limited amount of time, or limited storage. If you have Azure subscription that would definitely be the way to go.

Ahmed Anwar
  • 688
  • 5
  • 25
  • Actually I don't want to do that because I would like to sell this program one day so every user is going to have his own database on his computer. Can you tell me something for local database please. – Petar Bechev Jan 29 '16 at 08:08
  • You get a free subscription to Azure if you're a student though. Or if you have Dreamspark. – Ahmed Anwar Jan 29 '16 at 08:17
  • Why do you need each user to have a database on his computer? – aeee98 Jan 29 '16 at 08:46
1

If your connection string is like below

connectionString="Data Source=\v11.0;AttachDbFilename=|DataDirectory|\MainDatabase.mdf;Integrated Security=True"

then you should install sql localdb 2012.msi on your friend's PC. And make sure the .mdf file is located the same as on your computer.

Raidri
  • 17,258
  • 9
  • 62
  • 65
Sumon Tito
  • 105
  • 10