Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day, CodeProject Community!

I am currently working on my project and i need to link my database to my project by using the SqlConnection class. Here I need your help: How do I write the address of the database in such way that the program will only use the database that is in the same folder as it. I've tried to delete the "E:\Programs\C# Programs\Atestat" from "AttachDbFilename=E:\Programs\C# Programs\Atestat\Atestat\elevi.mdf ", but it would give me an error.

I'd greatly appreciate every answer.

Best regards!


EDIT1: Regarding all the answers given by now, I am very thankful for your implication, but I am afraid that neither of the answers would solve my problem.
The reason is that i need to transfer the program further on a CD. It is made to
prove my knowledge about C#.Having these conditions given, the scope of my program is to extract, update and insert + show some data from/into a database, no matter where this program will be located. ( I am using Microsoft Visual c# 2008)
Posted
Updated 13-May-15 6:16am
v3
Comments
Sascha Lefèvre 13-May-15 18:37pm    
I've updated my answer (solution 3), please take a look.

Don't.
If you insist that the database must be in the same folder as the application, then in production there is a very good chance your application will fail, as most installations put the app under the "Program Files" folder which is not write enabled by default to reduce virus activity.

Which means that the first time you try to INSERT or UPDATE the DB, you app will crash with an Access Violation Exception. Put it in a sensible place: Where should I store my data?[^] will show you where - and if you use one of the special folders and the Application GUID as the containing folder, it will be difficult for the user to meddle with as well.
 
Share this answer
 
I would recommend you to not use the "attach database through connection string"-feature in the first place.

Reason 1: It's only supported for SQL-Server Express editions. If you want your application to be able to use 'higher' editions of SQL-Server, it won't work anyway.

Reason 2: Microsoft recommends to stop using this feature because they are considering to remove it in a future version.

The alternative is completely trivial: Use SQL Server Management Studio to attach your database, making it a regular, permanent database. If you want to move your project to some other PC you can just either detach it or make a backup and attach it again on the other PC.

Edit after the question was updated:

It's possible to attach a database (an .mdf-file) programmatically. That is, you could let your application perform what I've mentioned in the paragraph above. In combination with my arguments for not using your current approach this could only serve to strengthen your point of proving your knowledge about C#, .NET and SQL-Server.

Please refer to this Codeproject-article:
Programmatically Enumerating, Attaching, and Detaching SQL Server Databases[^]
And, if needed, you could find further information on that with this Google search:
https://www.google.com/search?q=sql+server+attach+database+programmatically[^]
 
Share this answer
 
v2
The space in "AttachDbFilename=E:\Programs\C# Programs\Atestat\Atestat\elevi.mdf " is an issue

The problem with connectionstrings is that they have to make sense within the string as well as in your code.

A string is just a string, right?

Well, yes and no. Your program will not know if the syntax within the string is correct. It will just say "yup - that's a string".

The string parser doesn't know when a parameter starts or ends. Sure, everything after the '=' is the parameter value but where does it end?

The answer? Whitespace!
To the parser, it looks like this: "AttachDbFilename='E:\Programs\C#' blahblahblah"

To get around this, the parser accepts single quotes as strings:

"AttachDbFilename='E:\Programs\C# Programs\Atestat\Atestat\elevi.mdf'"

Try it and let me know.
 
Share this answer
 
Try this:
Get the executable startup path with Application.StartupPath property, than build dynamically the connection string:
C++
string ConnectionString = "AttachDbFilename = Application.StartupPath + "\\elevi.mdf";
 
Share this answer
 
v3
Comments
SnuKies 13-May-15 13:07pm    
I shall try this a bit later. I'll come back with the answers.


Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900