I have a little problem with my recent project. I have a connection string in my web.config but i'd like to access it in my sql specific class.
My connection string is looks like this:
"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Joblication-20180902120147.mdf;Integrated Security=True"
It's stored in my web.config file.
My problem is that the default asp.net functions can access this database but i'd like to store other data in the database so i tried to access it with SqlConnection class. I set the ConnectionString property of the SqlConnection object:
SqlConncetion connection = new SqlConnection()
connection.ConnectionString = "Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\aspnet-Joblication-20180902120147.mdf;Integrated Security=True";
But i get this error everytime:
System.Data.SqlClient.SqlException: 'An attempt to attach an auto- named database for file *.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'
I replaced the name of the mdf file with a '*' so it is shorter and easily readable.
If i understand this then my .mdf file is already attached to the MSSQLLocalDB so i should connect to the MSSQLLocalDB and i should be able to access the .mdf file somehow, right?
When i'm trying this:
connection = new SqlConnection();
connection.ConnectionString = "Data Source=(LocalDb)\\MSSQLLocalDB;Integrated Security=True";
Then it seems ok cause the connection is working now but my queries don't. My queries are trying to get data from the .mdf file's tables but the .mdf file is not specified in this connection.
So how can i specify it?
Integrated Security=Truein your connection string?