0

I am attempting to connect to a local SQL Server database in C#.

I am currently using the following connection string:

connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\User\source\repos\majorWork\majorWork\gameStats.mdf;Integrated Security=True";

However, I do not want to use a hardcoded file path, as I wish to be able to use the application across multiple computers, where the file path will be different. How should I go about doing this?

7
  • 1
    You should store the file to a specific folder which you can locate using path relative to the application path. For example you create a folder Database where the application is running. Then you can use relative path as AttachedDbFileName = .\Database\gameStats.mdf Commented Jun 14, 2020 at 5:34
  • stackoverflow.com/questions/37058684/… Commented Jun 14, 2020 at 5:41
  • Er, you can modify the connection string before you use it to create a connection.. "hardcode" a placeholder into it and replace the placeholder with the actual location of the db for the machine the program runs on Commented Jun 14, 2020 at 5:41
  • You should really put the database on the server - for which SQL Server is built - and then reference that server by its server name and let SQL Server handles all the file-related details. Commented Jun 14, 2020 at 5:52
  • Never use AttachedDbFileName when a database is attached to a SQL Server. It is not needed. Just remove the AttachedDbFileName it is not needed. Just use "Data Source" with server name and instance that you see on the login windows of SQL Server Management Studio. The database owns the mdf file and you do not have permission to read the file especially when mdf is on a remote machine. Commented Jun 14, 2020 at 9:26

2 Answers 2

1

Best way is set this connection in Web.Config file.

<Database>
  <ConnectionString name="connection">Server=servername; Initial Catalog=dbname; Persist Security Info=False; User ID=username; Password=password; MultipleActiveResultSets=False; Encrypt=True; TrustServerCertificate=False; Connection Timeout=30;;</ConnectionString>
</Database>    

Then add Add System.Configuration as a reference.

in C# you can call this

string constring = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

After that you can create new connection instance by passing this to

SqlConnection con = new SqlConnection(constring)
Sign up to request clarification or add additional context in comments.

Comments

0

If u install SQL server express using the default instance, then you can connect using . as your server name anyone can use it with default instance as well. 1.then in visual studio, click on solution explorer 2. Connect database, follow the instruction for SQL server 3. When it gets to server name use . to connect and choose your database name which you have created already in ms SQl, then test your connection 4. After it successful, u can now click on the database name showing under solution explorer, 5.after u click the database name, at the button right corner, there will be a connection string, copy it and use This will be declared publicly or globally

Sqlconnection con = new sqlconnection("paste the connection string");

And to use

Sqlcommand cmd = new sqlcommand("insert into......",con);
Con.open ();
Cmd.executenonquery();
Con.close();

4 Comments

This is a terrible solution. Do not try. Why uninstall the SQL Server. It make absolutely no sense.
No one is suggesting him to install, that how I use it and it working even for local host
He already has it installed. You are suggesting for him to uninstall. OP does not want to user LocalDb .See my comment in posting.
I simply said if he had installed the server using the default instance, not like he should uninstall and install again, OK have heard you, if u think that was what I meant, so be it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.