Unable to connect to Sql Server Express database - c#

I installed Sql Server Express 2008 R2, and created a database file in Sql Server management Studio. Now From Visual Studio 2008 I try to connect, but it fails with error:
Unable to open the physical file
"C:.....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
C:....mdf failed. A database with the
same name exists, or specified file
cannot be opened, or it is located on
UNC share.
...This is first time I use Express. I only used Sql Compact Edition before. Are there some settings here I need to figure out? It's not a duplicate name of other connection, and I have closed management studio completely. Still this error. What am I missing..?
If there are some settings, please give a little detail how I access them, as I'm not used to dealing with databases.

SQL CE is a file based database - SQL Express isn't.
It is a SQL Server - you can add it using the Server Explorer in Visual Studio.
The instance name is normally .\SqlExpress.

I faced the same issue today, and guess what daemon tools was the reason for this problem, so if you have installed daemon tools uninstall it and then try again.
Hope it solves your problem too.

You do not want to be connecting using the mdf. What you want to do is Open a SqlClient.SqlConnection and then using the SqlClient.SqlCommand execute queries on the database, and then read the information using the SqlClient.DataReader, just like you would with A SqlCE database.
If you are trying to add it to the Server Explorer you just need to connect to the database just like you would in SSMS (Management Studio) and then you can use it in your project.

If you created a database in SQL Server, you probably have it attached. In Visual Studio you cannot access this database as a file. You have to access it as an attached database (i.e. by its name).
Generally you can use database in files as well in Express edition. If you for some reason want to use file directly, then go to Management studio, right click on your database and detach it. Then you can connect to it from Visual Studio on file basis.

Related

SSMS, Visual Studio, SQL Server

Need Help please.
I have installed SSMS and Visual Studio.
I have 2 issues.
I am trying to learn SQL DBA and hence I installed SSMS only, I haven't installed SQL Server/Express.
While opening up a new query on SSMS, it asks me to enter the server name which I am unable to find and neither I can find instance name anywhere in my computer.
I have tried everywhere cmd and configuration manager.
Same with visual studio, when I try to create database, it doesn't allow me to add new local db as it doesn't show up when I try to add.
SQL Server is based on the client/server database system. SQL Server Management Studio is a management client, so you need a SQL Server installation in order to use it. You can install SQL Server Express for free which has a very similar feature-set to the paid editions (Standard and Enterprise Editions).
...in comparison to MS Access or SQLite which are a file-based database systems.
Visual Studio is an IDE which has some database tooling (SSDT: SQL Server Database Tools), while it can also be used for DBA tasks it is more appropriate for software development tasks using SQL Server than SSMS.

How to transfer the local database of wpf application from one computer to other?

I have created a local database in my WPF application,now i want to run the application on some other computer,now how to transfer the local database from one computer to other,and i dont want to create the database manually on other machine? Do we have to convert the database into some .exe file ? If yes,then how to do this or what is the best approach to implement this?
My application accesses its own database file in its own directory with this connection string:
System.Environment.CurrentDirectory + #"\Data\" + databaseFileName;
Please explain from scratch as i am new to this??
I suppose you're running your database in SQL Express. You can use SQL Server Management Studio (included in most SQL Server installers) to export your database, including data, to a .bacpac file.
In SQL Server Management Studio 2012, right click your database, select Tasks, then Export Data-tier Application... and go through the wizard.
Then connect SQL Server Management Studio to the SQL Express server of the target machine. You can do that by connecting Management Studio on your machine to the SQL Express running on the target machine (provided that a network connection exists between the two, and the target machine has been properly configured to accept the connection), or by also installing Management Studio on the target machine, then connecting it to the local server (.\sqlexpress).
Once connected, right click the Databases in the Object Explorer, select Import Data-tier application..., and go through the wizard.

How to query against a mdf file I attached in SQL Server in Visual Studio?

Hell all,
I just attached a mdf file in SQL Server 2008, and everything is working correctly. Now I want to open visual studio, and query against the mdf file I just attached, is there a local version of the connection string that needs to go in to web.config?
If I leave SQL Server open will it act as an instance of the database while I am working on the site in Visual Studio? Or am I suppose to run it somehow?
Any info would be appreciated, thanks!
Just use Data Source=(local) to connect to the local instance of SQL.
On a separate note, if it is a small app, consider using SQL Compact instead.

Deploying Windowsform application with SqlServer 2012 Databse in Visual Studio 2012

How can I deploy a windows form application with sql server database. I've searched a lot, The problem is I want the actual .mdf file when the application got installed on a PC for backup purposes. When I tried the publish feature, it converts the .mdf file into .mdf.deploy. I don't want to attach it into Sql Server Management Studio. I want to publish it with the actual application as .mdf, Kindly guide me in right direction. Thanks
If you nose around on the MS SQL Server site, you find that you can include a 'silent installer' for SQL Server in your .NET project. This will download and install an instance of SQL Server Express as part of your installation process. Just having the .MDF in a folder is useless.
In effect, you have to make at least a few items of functionality that stand in for SSMS: in particular database backups, database compaction, DBCC CHECKDB, etc. You are installing a 'full blown' SQL server instance, with it's separate windows service and system account, so you should be aware of how that impacts your user machine. This gets interesting if it's being deployed to a laptop, particularly a laptop that might sit in a café exposing ports to public LANs.
In general this is a can of worms. It is better to mandate the installation of SQL Server Express first, with SSMS, and merely attach your database to the existing instance. This makes a lot of other operations better behaved.

Add *.mdf file to C# Project

I'm using Visual Studio 2005. I create a project, not Web Project, just Windows application.
I remember that Access Database File can be added into a project. I don't need connection to server, data can be retrieved. And I want to do the same thing with SQL Database file.
I did the following steps:
Right-click on project.
Choose Add An Existing Item
Browse for *.mdf file.
DataSource Config Wizard appears and it displays this Message
An error occurred while retrieving the information from the database:
Failed to generate a user instance of SQL Server due to a failure int starting the process for the user instance. The connection will be closed.
I need help to add mdf file into my project.
To start with, and MDF file can be read only by an instance of SQL Server. If you deploy MDFs, then your application must either connect to a SQL Server provided by your end-user during setup, or it must deploy its own instance, in the later case a SQL Server Express Edition instance. See How to: Install SQL Server Express. With Visual Studio 2008 you can add a prerequisite to your own application setup MSI, see "Installing" the SQL Server 2008 Express ClickOnce Bootstrapper for Visual Studio 2008 SP1.
A second issue is that, despite the wide belief of the contrary, distributing the MDF alone without the LDF can land you into a world of pain. You can end up distributing an inconsistent MDF that needs the LDF to finish recovery and get into a consistent state.
But a more serious issue is your plan to deploy binaries (MDFs) instead of scripts for database deployment. This is doomed to fail. As soon as you'll plan to release v. 1.1 of your application you'll face the non-trivial problem of how to replace the user MDF (which now contains data added by the user) with your new MDF. This is why is much much better to deploy upgrade scripts always, and forget about the MDF in your project.
You can read from an Access file (*.mdb) in your app without any other requirements because the core Jet engine used by Access is included as part of Windows — it's built in. Sql Server is not included as part of Windows, and so you cannot use an *.mdf file in your app unless Sql Server has been installed and you have appropriate permissions for it.
It is possible to distribute either Sql Server Express Edition or Sql Server Compact Edition (recommended) with your app. Another option is SqlLite, which has a fully-managed database engine available.
An .MDF is a sql server DB, not MS Access. MS access is .MDB. You cannot read an .MDF on its own. It needs a log file (.LDF) as well. If you attach it to your local instance, it will create a new one for you. You can then connect to that DB.
To solve deployement problem (Updated version of your .mdf file and Code), you can have a utility in your application which can create .xls file of every table(Backup your database) which you used in your application. Now you can easly import those .xls file in SQL Server and create new version of .mdf file and attach same file in latest code.Now new release of your app ready to deploye..!

Categories