0

I am rather new to Database developing, and I am building a simple one to track Work Hours for our small company. Now that my program is done, I need to find a way to hook the Connection String on the Database that is located in a folder within the Local Network Server.

I am building a C# application which uses a SqlConnection.

My Database is a LocalDB, .mdf file.

How the program works:

  • Users install the program on their computers locally with the Setup.msi file
  • The program connects to the Local Server to access the Database (the drive letter depends on the PC. This is what's bugging me since I can give an absolute path)

Right now my Connection String looks something like this:

public static string ConnectionString { get; set; } = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TimeControl.mdf;Integrated Security=True;MultipleActiveResultSets=True;";

But this is no good as I was only using this during the actual programming for debugging purposes.

Is there a simple way to make it so that everyone can connect to the DB and access it, whatever their drive letter is? A relative path of some sort.

Thanks!

Chakraa
  • 3
  • 7
  • Can you use the ip 127.0.0.1? As in - `Data Source=127.0.0.1\MSSQLLocalDB;` – Alfie Goodacre Apr 19 '16 at 15:23
  • I thought 127.0.0.1 was a IP pointer to your local machine? I am trying to access the DB on a Local Network Server. – Chakraa Apr 19 '16 at 15:24
  • Why not put the file on a shared drive and just use the network path to the file? – Joakim Hansson Apr 19 '16 at 15:26
  • @JoakimHansson as written [Here](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.90).aspx), it says for the `AttachDBFilename` option: _Remote server, HTTP, and UNC path names are not supported._ – Chakraa Apr 19 '16 at 15:32
  • @Chakraa you are correct, I must have misunderstood the question, sorry about that! – Alfie Goodacre Apr 19 '16 at 15:34
  • @Chakraa I'd suggest doing what Käsebrot mentioned in his answer setting up a SQL server. If you want to setup SQL-Server you can find the connection string here: http://www.connectionstrings.com/sql-server/. If you decide to use trusted connection you need to have the users added to an active directory. In your case I would just use the top one "Standard security" and setup a remote account on the SQL-Server and replace the credentials in the connection string. – Joakim Hansson Apr 19 '16 at 15:42
  • @JoakimHansson yes I am following his idea right now. Just asking a couple of questions first since I don't wanna do accidents on my company's server. – Chakraa Apr 19 '16 at 15:47

1 Answers1

1

The proper way to do this would be to create a SQL-Server. Namely a machine which has the purpose to provide database services. There you could create a database everybody can access.

For further information read here. Here you can get an overview on existing versions of Microsoft SQL Server.

Another solution would be to make the user install SQL-Express along with your application (see here). This would write all the data to the database on their own machine. I guess you want to avoid this.

Community
  • 1
  • 1
チーズパン
  • 2,752
  • 8
  • 42
  • 63
  • Our PC-Server has Windows installed and we mainly use it as a backup server / put folders and company stuff in there. Would the installation of such a program disrupt our original use of the server? – Chakraa Apr 19 '16 at 15:33
  • As long as you have no SQL-Server installation on it there shouldn't be any problems. – チーズパン Apr 19 '16 at 15:34
  • Ok I see how to perform the installation, I will get on that right away. Do you have an idea as how to write the Connection String though? A way so I can remotely connect to the DB. – Chakraa Apr 19 '16 at 15:39
  • @Chakraa As soon as you have the SQL-Server installed you can use "SQL Server Management Studio" to get the connection string from your database. – チーズパン Apr 19 '16 at 15:41
  • And is SQL Server 2008 R2 the best option or is there a newer / better server? – Chakraa Apr 19 '16 at 15:45
  • There are newer, but for all the basic things 2008 should be enough. For further information on newer versions see here: http://sqlserverbuilds.blogspot.de/ – チーズパン Apr 19 '16 at 15:49
  • So, I have tried everything, since this morning. I haven't got it to work. I installed SQL Server 2008 R2 on my server PC, ran it, tried to attach my .mdf file to no avail. Tried again with installing the LocalDB platform on the PCs on setup installation, still to no avail. I am lost. – Chakraa Apr 19 '16 at 19:12
  • Ok, I think I realized something: My DB would be in LocalDB 2014. The connection string is MSSQLLocalDB instead of V11.0, that's how I am guessing. The thing is: my DB is pretty well packed right now. Do I really have to refill all of it or I can find a way around? My company already uses SQL Server 2012 for some of their DB. I wouldn't want to overwrite any of it. – Chakraa Apr 19 '16 at 19:20
  • `The database 'Z:\ADMINISTRATION\TIME CONTROL\LOGICIEL\BD\TIMECONTROL.MDF' cannot be opened because it is version 782. This server supports version 661 and earlier. A downgrade path is not supported. Could not open new database 'Z:\ADMINISTRATION\TIME CONTROL\LOGICIEL\BD\TIMECONTROL.MDF'. CREATE DATABASE is aborted.` (Microsoft SQL Server, Error: 948) – Chakraa Apr 19 '16 at 19:21
  • It seems like the sql server installation has a lower version than your .mdf file. Sorry but this is just a guess. Also I would be careful installing other sql instances while there are some already running. You will have to find a way to attach the .mdf – チーズパン Apr 19 '16 at 19:28