1

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?

13
  • 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. Commented Sep 17, 2018 at 9:40
  • Have Integrated Security=True in your connection string? Commented Sep 17, 2018 at 9:42
  • Yeah. Integrated Security is true in my connection string. Commented Sep 17, 2018 at 9:44
  • What's the latest error message? Would you share it please? Commented Sep 17, 2018 at 9:44
  • 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'.' Commented Sep 17, 2018 at 9:46

3 Answers 3

0

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.
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.
        }
    }
}

Comments

-1

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

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

3 Comments

"Try this" isn't an answer. Explain what you changed and how that will solve the issue.
It requires a localdb instance name and my only localdb instance is MSSQLLocalDB.
In your last code portion connection = new SqlConnection(); connection.ConnectionString = "Data Source=(LocalDb)\\MSSQLLocalDB;Integrated Security=True"; you won't specify your database name. mention your database name into Initial Catalog = aspnet-Joblication-20180902120147.mdf. i think it will work. I already implement with this.

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.