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.
Related
I was wondering what would happen to a program's connection to the database after compiling it to setup.exe? Will the data from the server explorer be saved somewhere after producing a setup installer so it could dynamically be changed later?
I'm still a bit new in coding. Just barely started.
So I'm not really sure if my questions would make any sense to all the experts.
I hope someone could enlighten me.
Thank you.
It all depends on what form of Database and what settings you had in your project.
case 1:
you are using a server-client Database. Server-client databases are like Sql-Server, MySql, Postgresql, Oracle.....
Usually they have a host, username, password and/or authentication method.
In this case, and while you are configuring your database with in your project in visual studio, you can choose to save the connection string with in your configuration file resources or hard code it in the application.
The Data will stay on the database server.
Case 2:
You are using one of file Database solutions like Sqlite, VistaDB, Acess DB file, XML based DB files, Sql-Compact SQL-CE (obsolete and replaced with SqlExpress)...etc.
In this case, the DB file (which should be in your project file and you have chosen from visual studio the option of (copy if newer)) will be included with your project with the data in it. You have to be sure in this case the proper assemblies are present with you your application *.dll. Your installer have nothing much to do here unless the DB solution you have chosen said other wise.
For example, if you used SqlExpress your installer should be sure that it is present when your user installs your application and if not it either installs it or instruct the user (at least by providing proper links) the methods to install it.
As for the connection string it takes the same conditions of case 1.
Hence you tagged your question with #sql-server I will assume you are looking for the SqlExpress case.
If that's the case I will refer you to the couple of links which will provide you with further details:
1- How do I connect to an MDF database file?
2-How do I connect to an .mdf (Microsoft SQL Server Database File) in a simple web project?
3-C# - Connect to MDF database
4-THE ‘CONNECTION STRING’ STRING OF CONNECTION WITH DATABASE. AN EXAMPLE OF USING IN APPLICATIONS
Finally remember web search is always your friend:
5-c# sql server connection string attach database file (in google search)
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.
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;
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
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.