Attaching (or not) .mdf file to SQLEXPRESS 17 using connString - c#

I'm working on deploying my software using Advanced Installer and I want to attach the .mdf file located in the [APPDIR] (client pc's destination folder). I came across this connection string:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname;Trusted_Connection=Yes;
This will attach with no problem when my app runs but, after its done, it will pop up an error "database mydbfile already exists, choose a diferent name" every other time the app is opened. In the description it says: "If the named database have already been attached, SQL Server does not reattach it. It uses the attached database as the default for the connection." But its not happening as its trying to attach it every single time.

What you are looking for is can be done by leveraging SQL Server Express User Instances
First, connect to your SQL Express instance using SQL Server Management Studio and manually detach the database. Then, update the connection string to include User Instance=true;

Related

Portability of an MDF database file across different Windows machines

Here's the context: I'm writing a C# application that connects to a local database file (MDF). The application's GUI enables the users to do various queries and view results in a Grid View control. The MDF file is distributed with the application and can be considered an immutable (or read-only) resource i.e. not meant to be updated or modified by the end user.
The application is currently in alpha stage and has never been deployed. The source code, along with the MDF database, is source-controlled (via Perforce), so when other developers in my team synchronize their workspace, they get a copy of the MDF database (and LDF log) along with the code. No server connection is involved, all queries are made to the local DB copy using (LocalDB)\MSSQLLocalDB and the default Windows Authentication.
Here's my problem: The MDF file cannot seem to be used anywhere but on the machine it was created on. If another developer builds my app on his computer, the connection to the local database fails with a generic SQL Server error 5171. However, the MDF file is not corrupted and is a primary DB file, so the error is elsewhere.
Here's what I tried:
Verified that SQL server version is the same on all machines, so it's not a compatibility issue
Connection string used in the application is dynamic (i.e. adapts to the location of the MDF file on the computer) and formatted as: Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\MyLocation\DatabaseFile.mdf;Integrated Security=True;Connect Timeout=30
Used same Windows login on all computers, to no avail (still gives error 5171)
Verified DB options with SQL Server Management Service: Restrict Access is set to MULTI_USER
The only workaround I found is to re-generate the whole database on the machine but that's not acceptable: can't one just connect to the MDF file one gets from Perforce and be done with it? Thanks.
Sorry to answer my own question so late after it was initially posted, but here it is: the solution is to make sure the MDF file is saved in Perforce as binary, not as text.
That solved the data corruption problem right away and the MDF database finally became portable across machines/users without any further issue.

Can not find which database is used for MVC 4 with Entity framework

I am trying to learn MVC 4 using C# and Entity Framework code first. I have created my model, create my database and populated it with the seed data. I have a ContextInitializer class which is going to drop and create the database each time I test. When I ran it, I can see my data populated on my site, I can edit, create and delete it. But I do not really know where it is stored.
I have looked at the webconfig file and saw the connectionString as follow:
"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-CCConsolidation-20140610143008;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-CCConsolidation-20140610143008.mdf"
I logged in my localdb and do not see that database at all. No error whatsoever. I tried to change the connectionstring and pointed it to my database on windows azure and still can not see the database anywhere. I do not see the mdf file in my App_Data folder either.
So my questions are 2 folds:
1) Where is my database hiding?
2) How do I deploy it to Windows Azure when time comes to do that? Will it be as easy as changing the connectionstring? I have tried that without success.
Please help!
NB: I have seen a few "similar" questions on this site but the answer pointed me to look at the webconfig which I did. I have traced to my local database but it is still nowhere to be found.
The question I was referring to is Where does ASP.NET MVC 4 stores accounts information?
Have I missed something?
Updated my connection string for Windows Azure:
"Data Source=xxxx.database.windows.net;Initial Catalog=CCConsolidation;Integrated Security=False;Database=xxxx;User ID=xxxx#yyyy;Password=xxxxxx;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;AttachDBFilename=aspnet-CCConsolidation.mdf"
Have anyone seen any issue with this connection string? I used this in the Webconfig to replace SQL express, but the database was not created.
LocalDB is a new functionality in SQL Server 2012. It's a simple command line executable (not a service) that can start and stop DB instances on demand. This has nothing to do with the App_Data of your application, and the old AttachFile.
You can start, stop, delete or create instances using the command line, for example:
SqlLocalDb create "MyInstance"
SqlLocalDb start "MyInstance"
SqlLocalDb info "MyInstance"
SqlLocalDb stop "MyInstance"
SqlLocalDb delete "MyInstance"
If you want to connect to your DB from SSMS, you need to specify the following DB:
(localdb)\MyInstance
The advantage of this DB is that you don't need to install a whole SQL Server instance, start and stop services, create logins, grant permissions, and so on.
The database files are inside your Windows user profile folder, and the process is run under your own credentials, so you don't need to give permissions to any folder either.
You can get plenty of info on LocalDB here: Getting Started with SQL Server 2012 Express LocalDB
Your database is not living on a local instance of SQL Server (e.g. localhost) but appears to be a SQL Server Express instance fired up with your application and the database is stored in the aspnet-CCConsolidation-20140610143008.mdf in the App_Data directory of your site.
You should be able to browse that datastore from within Visual Studio by double-clicking on the mdf file in your Solution Explorer

C# / SQL Server - can't have two connections simultaneously to one database?

If I have SQL Server Management Studio open to my database, I can't open the database in my C# program because it will throw an error. I have to close the connection in Management Studio.
Is there a way to be able to connect to a single database with multiple connections?
This is just a local .mdf file on my computer that I'm working with.
The problem is that you connect with a user instance, which is single-user only and normally used just for development:
User instance scenarios include:
Any single-user application where sharing data is not required.
Reference: http://msdn.microsoft.com/en-us/library/ms254504.aspx
So your default SQL Server instance has the database open in your Management Studio session. When your program creates a new instance it can't access the database file because it is open with a different instance.
You can try using your server tool in Visual Studio to manage your database instead of Management Studio.
You should avoid connecting directly to the .mdf file. Have SQL Server host the database, and then connect to it from there rather than the file.

How to make SQL Server Connection (not from file) appear in ADO.Net Connection Model Wizard?

I am trying to add a connection to Adventureworks on a new development machine running VS 2010. I have SSMS running and am able to query the Adventureworks database. However, when I click “New Connection” (steps followed below after *) , I do not see SQL Server connection as an option. Is there a way to configure VS 2010 so that it sees my localhost connection to SQL Server?
Second option:
When I try using the option Microsoft SQL Server from database file, whenever I browse to the mdf file for Adventureworks, I always see the message 'file in use', even though I have only one instance of VS 2010 open and SSMS is closed.
steps i followed were
new project
class library
right click solution explorer
add
new item
ado.net entity data management
new connection
Just type in localhost as the server name, test the connection and click Ok. It will be saved, next time it will appear in the list.
The Data Source type is set to "Microsoft SQL Server (SqlClient)"
If this doesn't pop up in your screen then install SQl Server Native Client here:
http://msdn.microsoft.com/en-us/sqlserver/ff658533.aspx

sql server and file monitoring

i have a problem with a sql database. if i open it in smo then i can't access it in vs.net because i get the famous error "another process using this file" so before i could open it in visual studio i have to detach it from smo database list.
here is my question, how sql can monitor the mdf file if i keep it detached from its database list?
i mean it should somehow monitor the database file to be able to use the automated periodic tasks that has been set by db administrator right?
I think your real problem is in how you're opening the database in Visual Studio. You need the database server to be attached to the file to continue running the jobs. Visual Studio is able to connect to the database server instead of opening the database file. Try viewing the "Server Explorer" window and add a connection there.
Edit:
Since your question is tagged with "asp.net," I take it that you're not talking about Visual Studio opening the file, but rather about your ASP.NET code opening the file. My answer there is similar: use a connection string in your web.config that specifies a connection to your server instead of a connection to the file. http://connectionstrings.com/ can help you figure out what your connection string should look like.

Categories