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

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.

Related

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

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;

Locate database

I have used SQL Server Management Studio to convert an Access file to SQL Server. I want to add this database to a project using C# (it appears in Server Explorer - Data Connections).
When I use C#, Solution Explorer - Properties - AddResource - Add Existing file I am required to navigate to the database file, but I cannot locate it.
Database is RWJ-PC.PaulsNewDatabase.dbo. Please advise if possible.
Somewhere on your hard disk (or in a network folder)is a file with an extension of mdf. Attach the file to your SQL Server (using SQL Server Management Studio) instance and you will have a a database. SQL Server databases live on the file system as a mdf and (possible one or more ndf) file(s). It lives in memory and can be used when attached to a SQL Server instance.
C:\ProgramFiles\MicrosoftSQLServer\MSSQL12.MSSQLServer\MSSQL\Data\Filename
Found eventually by trolling the internet
If you have imported the data into an instance of SQL server you can connect to the instance of SQL or to the mdf. Look here --
How do I connect to an MDF database file?
Search for connecting to SQL Server with C# (VB.Net). You can use Entity Frame work or connect directly.

Connect to LocalDB of other system [duplicate]

I am looking into using the new SQL Server Express LocalDB (I think it is code named "Denali") for a desktop application.
It is currently running with SQL Compact, but the user is wanting to share the database between multiple PCs on a network. Unfortunately this is not something that SQL Compact can do, so I am investigating other solutions.
The client requires the ability to send database files easily to other sites or to back them up to a flash disk, so I am avoiding going to SQL Express because there is quite a bit of "administrator" knowledge required to backup and restore.
So, my questions is, does the new SQL Express LocalDB support remote connections to the database over a network and/or through a shared network folder with the mdf file in it?
LocalDB does support supplying a path for an attached local DB in it's connect string (AttachDbFileName) hence the shared network folder option.
NOTE: This question pertains to "LocalDB" the new version of SQL Express 'Denali' and not to SQL Server Express 2008 or prior.
See article here announcing LocalDB's release: http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx
No, SQL Server Express LocalDB doesn't accept remote connections.
The idea with shared network folder might work, but only if you are able to make sure the LocalDB instance is shutdown before you try to copy the file. Also keep in mind that only one LocalDB instance can have any given database file open at the same time. and don't forget about the log files!
Additional security warning: unlike SQL Server Compact databases, SQL Server Express databases (including LocalDB ones) are not designed as secure data exchange format. For instance, they can contain malicious code in .NET assemblies embedded in them. So you should never open databases from untrusted source.
Maybe providing the customer with a simple tool that automates the backup process would be a better idea?
This isn't a fresh thread, but I would like to share my experience with SQL Server Express database LocalDB.
I have a WPF C# project using SQL database with LocalDb Engine. It is working fine no problem, I can use the database with the WPF app. I wanted this app to work on network with more PCs.
On the network another PC can use the database from my PC using UNC path in the connection string.
It seemed to me the remote connection is working. However when the remote PC is connected, I am not able to use the database with my local WPF app. If I run my app first the remote PC could not connect. So this tells me that the remote connection is working, but the multiple connection is not allowed.
OK, I didn't give up and I run the app from my PC twice and I saw it is working which tells me that the same SQL LocalDB engine can handle multiple connections locally only.
I hope this experience will help someone. Thanks.
In short, yes it can. Here is a tutorial on how to configure it.
Also, here is another post with a potential issue that might occur.
Both explain how to configure SQL Server Express to accept Remote Connections.

How to use backup .sql file to add databse to sql server using C# console application?

I was working on a project where i made a database on sql server 2008 using visual studio 2010 and i took backup of that databse by right clicking on it in sever explorer and selecting "Publish to Provider" that creates a .sql file in my computer that i taken. Previously i formatted my pc and sql server databases are lost, so now i want to use that .sql file to recreate the database with all data, but i don't know how it can be possible.
Something i tried but it created all tables in master database but i want the database of the name i.e. stored in .sql file or specified by me.
Please help.
First Create a Database using this,
Create Database YOURDBNAME
Here, YOURDBNAME = DataBase Name
Then, Open your .sql file write this in First line of the .sql page,
Use YOURDBNAME
Now press F5, all the tables and data will be created and inserted.
You can use the SQL Server Management Objects libraries to manage all aspects of SQL Server.
SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server. SQL Server Replication Management Objects (RMO) is a collection of objects that encapsulates SQL Server replication management.
From your description, however, this can be overkill. You get simply use SQL Server Management Studio (link goes to the free Express edition download), connect to the server, open a new Query Window with your SQL file and run it (F5).

Connecting to sql server database mdf file without installing sql server on client machine?

I am creating a window application that need to use sql server database.
I want to install this application to client machine without installing sql server so that my application can still connect to a database i.e mdf file that i will be providing at client system.
How can i connect to a database(mdf) on client machine through my window application without installing sql server. ?
I dont know is it possible or not.
If possible what will be the connection string in that case. Database need not be used in network.
Client mahine dont need any installation. Every thing needs to be run through pen drive
.mdf files are SQL Server database files. No other application can understand, read or update those files. If you need to open and mdf, you need a SQL instance. That instance can be local, can be an Express edition, or can be a remote one, doesn't matter. If your application needs a local SQL instance for it's own use then it can install SQL Server Express Edition when deployed.
You can try Sql Compact Edition or SqlLite. I think these are just a file based solution.
In that case you must have a server machine where your database files are stored. For that reason you have to use SQL Express Edition 2005 or 2008 in one machine and SQL server management studio to manage your database. Those are all free from Microsoft. The client machines will be connected through the connection string. Those machines don't need SQL instance or SQL server installed.
Regards..
You can search your MS-VS CD for SQLEXPRESS (please note the capital letters) and include it in your software when you create the set-up and your problem will be solved.
You don't have any other way except to use another database like access.
This database doesn't need its software.

Categories