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.
Related
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.
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!
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.
I'm having a problem switching a project I am working on over to another system. It's an ASP.Net web application and web form wrapped in a single solution, with a SQL .mdf DB file in the project as well. I'd like to be able to zip up the whole thing and move it around, but when trying to debug the solution on the other machine I get an error about the local DB not existing. My connection string in the Web.Config file looks like this:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-name-of-database.mdf;Initial Catalog=aspnet-name-of-database;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I know I must be making an obvious mistake, but I can't figure out what it is. Any advice, please? Thanks.
Edit: The specific error is:
"The specified LocalDB instance does not exist."
Run the following from your package manager console:
SqlLocalDb info
If the executable isn’t found, chances are you do not have LocalDb installed in your new machine. You can use the steps mentioned in the below link to debug as to why the connection is not successful to your localDB:
http://odetocode.com/blogs/scott/archive/2012/08/15/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True
Try setting the Datasource as below.
Data Source=.\;
If you are able to connect to your database, then the set the ServerName as DataSource in your connection string.
Make sure you have SSMS installed in your new system.
Download SQL Server Express 2014
This is my connection string:
<add name ="myConfig"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Platform.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
and the class that uses that connection string:
internal static SqlConnection Connection
{
get
{
AppDomain.CurrentDomain.SetData("DataDirectory",#"C:\Users\Marius\Documents\Visual Studio 2012\Projects\BidingPlatform2014\BidingPlatform2014\Platform.mdf");
return new SqlConnection(myString);
}
}
I am using Visual Studio 2012 and SQL Sever 2012.
But after my application closes data isn't saved into my primary database (database that I see under Server Explorer > Data Connection).
I think that data is stored during the execution into a copy of database located under bin\Debug folder.
How I update data into my original database.
Sincerely,
First, just do the call to AppDomain.CurrentDomain.SetData just one time when your app starts and before any activity on the database. There is no need to repeat it (and has no effect once the first call to the database is made) every time you ask for the connection.
Second, |DataDirectory| should be a folder, not the full path to the MDF file
AppDomain.CurrentDomain.SetData("DataDirectory",
#"C:\Users\Marius\Documents\Visual Studio 2012\Projects\BidingPlatform2014\BidingPlatform2014");