local .mdf database connection string - c#

I've been trying to make a address book with a local .mdf database but I can't find any working connection strings.
My current connection string
Server=.\\SQLExpress;AttachDbFilename=|DataDirectory|Database.mdf;Database=Database;Trusted_Connection=Yes;
When I try to open the connection I get and exception saying:
An unhandled exception of type 'System.Data.SqlClient.SqlException'
occurred in System.Data.dll
Additional information: Directory lookup for the file "C:\Users\******\Documents\Programmming\C#\Projects\Address Book Sql\Address
Book Sql\bin\Debug\Database.mdf" failed with the operating system
error 5(Access is denied.).
Cannot attach the file 'C:\Users\*******
\Documents\Programmming\C#\Projects\Address Book Sql\Address Book
Sql\bin\Debug\Database.mdf' as database 'Database'.

Fast solution
Try to run your Visual Studio as Administrator.
Right click on the Visual Studio icon -> Run as administrator
Another solution
Check the permissions on the folder "C:\Users******\Documents\Programmming\C#\Projects\Address Book Sql\Address Book Sql\bin\Debug\"

Also, make sure that you close the connection to your .mdf from the Server Explorer.

You could install SQL Server LocalDB and host the mdf so that you don't have to worry about the path in the connection string. Hosting mdfs in LocalDB can be done through command or through code.
Its easier to manage the permissions as well.

Related

SQL Server network interfaces error 52- Unable to locate a database at runtime installation

I am creating a POS system in Windows Forms (C#) in which I use a SQL Server database file (.mdf) to store items (completely offline). When I install the application on my computer, it works fine, but when I install it on my clients PC, an error happens:
(provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation"
I read somewhere that the problem is caused due to the fact that the connection string of the database of my client's PC is different. I tried to add the connection string dynamically on runtime but again it only worked on my computer.
Another reason that might be causing the problem is that I used 'server-based database' since local database option isn't available in Visual Studio 2017 for some reason.
Another solution I looked up stated that I should install SQL Server Express on my client's PC. That also failed (maybe I have to set it up in a way or something).
I also tried adding the database.mdf and database_log files in the setup folder.
Lastly I tried installing 3rd party installers (Advanced installers 15.8 and InstallShield Wizard in VS 2015) which also failed.
(I have provided the code for the connection of database taking place and the connection string)
public void ConnectToDB()
{
DBConnection = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\SAIM NASSER\Desktop\app layer\data layer\Database1.mdf; Integrated Security = True");
DBConnection.Open();
ResultSet = new DataSet();
}
If I understand you correct, you want to use LocalDB
That means using Sql Server without installing a full sql server, but just the localdb part from sql server express.
For this to work you need to install the LocalDB Driver, which can be found here
https://www.microsoft.com/en-us/download/details.aspx?id=29062
You need only the ENU\x64\SqlLocalDB.MSI
This is the only thing you need to install in your clients computer, I believe it can also be installed silent, you have to research a bit for that.
And yes, you also should change the connection string on the clients computer, you need to alter it so it points to the MDF file on the clients computer, because that location will probably be different then on your computer
EDIT
To get the connection string working, you can try this
On the clients computer, create a text file and rename the extension to .udl
So for example you have a file test.udl
Now from explorer, double click it, this will open the datalink editor.
From here you can enter/choose different settings, and click on the test connection button.
Once you get a working connection, save it, and open this file with notepad.
Inside you will find the working connection string
Hope this helps

Issue with .mdf and installshield

I have a program that connects to .mdf using SQL Server 2014 localdb. It needs to install to program files so it can't use click once. That being said, when I build it with click once. it works perfectly np.
However when I install it via install shield i get this exception
The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Program Files (x86)\King Canine\King Canine Software\CanineDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
I have the SQL Server 2014 LocalDB as part of the redistrib selected and it is installed on the computer I'm testing it on.
Has anyone ever had this issue before or have any idea?
Based on the error message you posted, it can mean either of the following:
A database with the same name exists
this means that the database you are trying to attach already exist on the database.
specified file cannot be opened, or it is located on UNC share
you can try to check if the folder of the .mdf file you are trying to attach has enough folder access rights, you can see this on the folder's properties, in the security section.
Change the connection string like this:
string connectionStr = #"Data Source=(LocalDB)\MSSQLLocalDB; Database=CanineDatabase.mdf;Integrated Security = True";
Make a T-SQL query for make Database in server:
USE [master]
GO
IF NOT EXISTS(SELECT * FROM sys.databases WHERE name = 'CanineDatabase.mdf')
BEGIN
EXEC sp_attach_db #dbname = N'CanineDatabase.mdf',
#filename1 = 'C:\Program Files (x86)\King Canine\King Canine Software\CanineDatabase.mdf',
#filename2 = 'C:\Program Files (x86)\King Canine\King Canine Software\CanineDatabase_log.ldf';
END
GO

Windows form SQL Server connection string issue

I have a Windows Forms application written in C# using SQL Server 2012 Express as its database.
I need a connection string to work on any PC.
I try to put the .mdf file in DataDirectory or drive C: or C:\ProgramData
DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydb.mdf;Pooling=False;
In this case the error is:
Failed to update .mdf database because the database is read-only
If I try to use
user instance=True;
in the connection string, I get the following error :
System.Data.SqlClient.SqlException (0x80131904): Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
If I try to put the .mdf file in user application data AppRoaming folder it's not supported and returns error :
An attempt to attach an auto-named database for file failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
So how to set my connection string and where should the .mdf file be stored?
Please help me
And thanks in advance

Visual Studio 2013 DB creation error : a network related or instance specific error occurred while establishing connection with sqlserver

First of all, let me tell you that I have tried every damn thing by Googling to overcome this error and I have not done any TYPO error either. But, none of the Google results helped. Let me tell you the scenario... I installed Visual Studio Ultimate 2013 (and nothing else, no separate sqlexpress, nothing). All non-DB apps are working fine. But, for one project, when I tried to create new database connection (by right clicking 'data connections' or clicking 'connect to database' icon for creating localdb), I got this error:
"A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (Provider:
sqlnetwork interfaces, error 26- Error locating server/Instance
specified)"
Then, as usual, I tried to Google for it. But most of the answers were related to sqlexpress (which I didn't installed separately). Then one answer was about "sqlserver configuration manager" and enabling sqlservices and something called surface area.... But, as "sqlserver configuration manager" was not there in my 'all programs' on my windows 8 (64x) machine, I downloaded sqlexpress and sqlserver management studio too. But an item named something like "surface area manager...." was not there. I enabled sqlservices, all tcp/ip ports. Shut down my firewall. Still getting the same error.
NOTE: I dont have any connection string in my webconfig till now because I was just going to create the new DB. So the the answer about editing conn string doesn't mattered to me.
NOTE: This was not the case with Visual Studio 2012. On only VS2012 (and nothing else) installation, it worked fine for creating the DBs and all other things. So, what's wrong with VS2013??
Server \ Instance name must be reachable from your client, as the creation of the connection string will try to verify that it connected OK. Try this from your workstation:
sqlcmd -Sservername\Instance_or_IPaddress_of_Sql_server -E -Q"select ##version"
Until you get a proper server name or instance, you will not be able to successfully create a db connection.

Can't connect to SQL Server in visual studio 2012

I am trying to connect to an SQL server database in visual studio 2012 but having no luck. I have the following code.
var db = Database.Open("anagram_database");
var shows_data = db.Query("SELECT * FROM sorted_words");
the database name is correct without any typo's, I am receiving the following error
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I am completely out of my depth with this and don't even know where to start. I cant seem to find anything with googling.
From the documentation:
name
Type: System.String
The name associated with the database to open. name can specify an .sdf or .mdf database file that is in the App_Data folder. (Do not include the file-name extension.) Alternatively, name can specify the name of a connection string in the Web.config file.
Here, App_Data means the logged in user's Application Data folder (or actually, the user running the application, which is probably the same). Type %APPDATA% in the address field of a Windows Explorer and press enter. The folder that opens is where you should place the anagram_database.mdf database file.

Categories

Resources