1
votes

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?

3
Oh...I found the way to specify my database. I added Initial Catalog to the ConnectionString property but now it fails to log me in. Do u have any idea how could i login? I think it's windows authentication based by default but i'm not sure... I would greatly appreciate any help. - Dániel Foltán
Have Integrated Security=True in your connection string? - TanvirArjel
Yeah. Integrated Security is true in my connection string. - Dániel Foltán
What's the latest error message? Would you share it please? - TanvirArjel
System.Data.SqlClient.SqlException: 'Cannot open database "aspnet-Joblication-20180902120147.mdf" requested by the login. The login failed. Login failed for user 'DESKTOP-BIDR6TV\gomis96'.' - Dániel Foltán

3 Answers

0
votes

First in your web.config update the connection string as exactly as follows:

<add name="DefaultConnection"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFileName=|DataDirectory|\aspnet-Joblication-20180902120147.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />

Then in the code:

// Don't hard coded the connection string here. Get it from web.config as follows
string connectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
   connection.Open();
   // Do your necessary staffs here.
}
0
votes

Add in your Web.config

<add name="Connectionstring"
providerName="System.Data.SqlClient"
connectionString="Data Source=(localdb)\ProjectsV13;Initial Catalog=TestDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" />

 class Program
{
    private readonly string _connectionString;
    public Program()
    {
        _connectionString = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
    }
    static void Main(string[] args)
    {

        using (var connection = new SqlConnection(_connectionString))
        {
            connection.Open();
            // Do your logic here.
        }
    }
}
-1
votes

In your code portion add database name.I already implemented this way.

"Data Source=(LocalDb);Initial Catalog=databasename;Integrated Security=True;"