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
Related
I have written an application that uses SqlClient and a local mdf database. However, it does not work on another machine (throwing an exception that the server cannot be run) and I assume this is because there is no SQL server installed? But I thought that using local database and SqlClient elimites the need for the server, something like the old SQL CE. Is that the problem?
EDIT Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing 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
As for connection string, I keep the one generated by Visual Studio Express, just modify the path to use the app folder.
SqlConnection s = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ="+ System.IO.Path.GetDirectoryName(Application.ExecutablePath)+ #"\Database1.mdf; Integrated Security = True");
Is there a simple way how to rework this so that my app does not require SQL server installed? All I need is to have it use the local database file in its folder.
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
I have lost connectivity using Visual Studio 2012 database...
when i try to reconnect and run the application then here us the Exception
NOTE: The previous file was deleted mistakenly, and trying to attach the new one it says it already exist?
what is the Solution?
What kind of database are you running? Specify:
If you have SQLEXPRESS Database:
First you need to change your Data Source name like this.
"Data Source=(LocalDB)\v11.0" to "Data Source=.\SQLEXPRESS"
To connect to your local SQL Server instance, issue the following string:
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname Trusted_Connection=Yes;
To connect to your local SQL Server instance with a database file located in a data directory, issue the following string:
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname Trusted_Connection=Yes;
To connect to LocalDB Automatic Instance, issue the following string:
Server=(localdb)\v11.0;Integrated Security=true;
To connect to LocalDB Automatic Instance with a specific data, issue the following string:
Server=(localdb)\v11.0;Integrated Security=true; AttachDbFileName=C:\MyFolder\MyData.mdf;
To connect to LocalDB named Instance, issue the following string:
Server=(localdb)\MyInstance;Integrated Security=true;
You need to get a string that matches your specific DB exactly, for it to connect.
In you connection string just add the 'database='. This prevents SQL server to create the auto-named database.
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.
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.