System.Data.SqlClient.SqlException: 'An attempt to attach an auto-named database for file "X" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.'

Hemanth B 886 Reputation points
2022-05-03T06:53:13.913+00:00

Hi I am using WinAppSDK WinUI 3 framework. I created a Service Based-Database and in it I created a table. Now when I run the app I am getting this:
198389-image.png

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,825 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 41,006 Reputation points
    2022-05-03T07:19:16.49+00:00

    I created a Service Based-Database

    But in your connection string you use "AttachDbFilename" for a SQL Server User-Instance; not service based DB: it should be "Initial Catalog" or "Database" parameter instead.
    And the error is raised, because the database (file) already exists in SQL Server.
    See https://www.connectionstrings.com/sqlconnection/

    0 comments No comments

  2. CathyJi-MSFT 21,096 Reputation points Microsoft Vendor
    2022-05-04T06:45:25.467+00:00

    Hi @Hemanth B ,

    According to your error message, please just to be sure check again your database server as it might already contain a database with the same name. Or please check if the file location of this database is correct.

    In addition, please check if the parameters of your connection string is right. Please check below blog to get more.

    An attempt to attach an auto-named database for file ....database1.mdf failed


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments