An Attempt to attach an auto-named database Error WPF Application - c#

I am getting the following Error:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Details: An attempt to attach an auto-named database for file Path\pharmacyDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Here is my connection string in app.config file:
<connectionStrings>
<add name="PharmacyManagementSystem.Properties.Settings.pharmacyDBConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\pharmacyDB.mdf;Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
I already did search for similar issues and found solution but non of them work for me until now, here is what I did:
following this article: A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. in msdn forum and this one: An attempt to attach an auto-named database for file …database1.mdf failed also this one: An Attempt to attach an auto-named database Error in the Stack.
then I tried out to change my connection string couple times as follow:
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;Database = pharmacyDB.mdf;Integrated Security=True;Connect Timeout=30"
but it give me this error this time:
Cannot open database \"pharmacyDB.mdf\" requested by the login. The login failed.\r\nLogin failed for user 'computername\username'.
hint: the folder access is full control for the current user in Security>>Permissions for username
then I modified my connection string for 2nd time as follow:
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\pharmacyDB.mdf;Integrated Security=True;User Instance = True;Connect Timeout=30"
but it still gave me this error :
The user instance login flag is not allowed when connecting to a user instance of SQL Server. The connection will be closed.
also I can't give a fixed path to connection string because I can't be sure where my application data will be in the end user computers. End user can modify the default installation folder Program Files as per my requirements.
EDIT: tried the fixed path in connection string just to see if it works while debugging phase but it gave me the same first error. Also my Database file is copied always so I tried out to delete the original file after debugging so I have only one database with that name in my computer and run the App from the debug folder but it crash on the same point that it fire the exception from within VS.
Can you please help me or direct me into the right way to follow Please!

Related

How to create a setup for a project which includes Entity Framework Code-First

I've built a Windows Form App project and now I wanna create a setup which is going to be used all computer. I published my project, and after I had clicked my exe, it's not working. I'm taking a error message like below.
error picture
error is going on like System.Data.DataException:
System.Data.DataException: An exception occurred while initializing
the database. See the InnerException for details. --->
System.Data.Entity.Core.EntityException: The underlying provider
failed on Open. ---> System.Data.SqlClient.SqlException: Cannot attach
the file 'C:\Users...
My way:
1- I am using localDb in app-config file, so I've organized conection string for it.
2- I put myDb.mdf into my project, after I'd set its "restrict address" for single-user and I'd detached.
3.I published my project.
<connectionStrings>
<add name="DatabaseContext" connectionString="Server=DESKTOP-N01M7RV;Database=DB_GOPCBS;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>

Cannot create file '*.mdf' because it already exists

I am using Entity-Framework in a Winforms application. The application connects to MSSQLServer and everything works fine. Then I detach the database and copy the .mdf file to the app folder and change the connection string to use the local .mdf file, still everything works fine.
The problem is that when I copy my application together with the database to another PC suddenly EF tries to recreate the database and throws this error:
Cannot create file 'Path\MyDatabaseName.mdf' because it already exists.
Change the file path or the file name, and retry the operation. CREATE
DATABASE failed. Some file names listed could not be created. Check
related errors.
Context Initializer is set to MigrateDatabaseToLatestVersion:
Database.SetInitializer<MyContext>(new MigrateDatabaseToLatestVersion<MyContext, DAL.Migrations.Configuration>("MyConnectionString"));
Also, automatic migration is off and database is created by EF Code-First on Sql-server and is already updated to the latest migration so no migration is needed.
Connection string for the first scenario is:
<add name="MyConnectionString" connectionString="Server=DESKTOP-XXXXXXX; Database=MyDatabase; Integrated Security=True; Connect Timeout=30;" providerName="System.Data.SqlClient">
and for local .mdf file is:
<add name="MyConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\MyDatabase.mdf; Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
So, What is going on here??
In my case, go to C:\Program Files\Microsoft SQL Server\SQLSERVER\MSSQL\DATA folder and delete the same database name '*.mdf' file if exist. After deleting it and recreate the database, the problem is resolved.

ServerVersion = 'conn.ServerVersion' threw an exception of type 'System.InvalidOperationException'

I've seen this error answered for other people, but I can't seem to get it to work. I'm trying to connect to a SQL Server Database project locally.
I'm getting this error thrown on Open() when I do the follow:
SqlConnection conn = null;
conn = new SqlConnection(connection)
conn.Open()
Where connection is a connection string passed from my webconfig. I've found examples online and tried these three connection strings and I get the same error every time:
<add name="TICKETING_CONNECTION" connectionString="Server=(local); DataBase=Ticketing_DB; Integrated Security=SSPI"/>
<add name="CONN"
connectionString="Data Source=localhost; Integrated Security=SSPI;Initial Catalog=Ticketing_DB"
providerName="System.Data.SqlClient" />
<add name="CONN2"
connectionString="Data Source=(local);Initial Catalog=Ticketing_DB;"
providerName="System.Data.SqlClient"
/>
Could the problem be stemming from the path of where the database is located on my machine? I've tried copying the c:\ path but didn't have any luck. I've seen some examples online use a .mdf file, but my project does not have this file either.
This is my first time trying to connect to a database project, so any help or advice would be greatly appreciated
If you are using Visual Studio try going to the Server Explorer and Right click on Data Connections and Add new connection by selecting DataSource as Microsoft SQL Server and provider as .net framework provider for SQL Server.
Under the server name check for the SQL Server if it is listed with your machine name.
Then try to provide the database details and user name and password for SQL Authentication or select windows for windows authentication. Then do a test connection. If it is fine on click of OK it will add the data connection.
Right click the properties of data connection which is newly added and navigate to properties to extract the connection string. Compare it with yours. This can help you troubleshoot the issue.

"Unable to open physical file" when opening the database

I'm working on a project in C#/ASP.NET and I've come across an error which I can't solve. I have a database in which I created some tables, and whenever the application tries to open the database, I get the following error:
"Unable to open the physical file "path\App_Data\Cars.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)". An attempt to attach an auto-named database for file path\App_Data\Cars.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."
I have SQL Express 2014 installed,named the instance 'SQLEXPRESS', wrote this instance ID in the Visual -> Options -> Tools -> Data Connections. In the web.config file, I have :
< connectionStrings>
< add name="ConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\Cars.mdf';Integrated Security=True;User Instance=True"/>
< add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename='path\App_Data\AspnetDB.mdf';Integrated Security=True;User Instance=True"/>
< /connectionStrings>
And the way I am trying to open the database is:
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Cars.mdf;Integrated Security=True");
con.Open();
Can you please tell me what to do? Thanks!
UPDATE: Managed to solve it, the argument in the SqlConnection should have been the same as the ConnectionString in web.config. Now there is another one: When I try to open the default database that Visual Studio provides(with Users,Roles,ecc), I receive the following error: The database „path\App_Data\AspnetDB.mdf” cannot be opened because it is version 852. This server supports version 782 and earlier. A downrade path is not supported.
Could not open new database „path\App_Data\AspnetDB.mdf”. CREATE DATABASE is aborted.
An attempt to attach an auto-named database for file'path\App_Data\AspnetDB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share." Any ideas?
Taken from OP's edit to the question:
Managed to solve it, the argument in the SqlConnection should have been the same as the ConnectionString in web.config.

Hosting ASP.NET Web App on IIS DB connection problems

I have used IIS before, but I've never used a database and I've never done a web app. I did the same steps for putting a site onto IIS and everything works EXCEPT for when I go to a page that has a DB connection on it. I've tried a lot and this is all I get.
A network-related or instance-specific error occurred while establishing a >connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
Windows Application Event Log:
Cannot get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user.
This was built in Visual Studio and I have a SQL Express DB.
I posted my connection strings, any help is greatly appreciated.
I have added this to my applicationHost.config:
<add name="DefaultAppPool">
<processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
And now I get:
An attempt to attach an auto-named database for file |MY DB1 FILE| failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB1.mdf;Initial Catalog=aspnet-ICCAA_VIZIER-20150223043129;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="ConnectionString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB2.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>
I was able to figure it out. I changed all my data connections to .\SqlExpress. Which led me to another error of:
Login failed for user ‘NT AUTHORITYNETWORK SERVICE‘
To fix this I followed this guys article to figure it out.
Login Failed For user NetworkService

Categories

Resources