struggling to connect a sql server database - c#

I am working on a package installation for my company. We have a product that is used offline, so a local database is needed. My install package moves a .mdf and .ldf file to it's proper directory, and my connection string will attach the database if it is not attached:
"Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Program Files\\Microsoft SQL Server\\MSSQL11.SQLEXPRESS\\MSSQL\\DATA\\myDatabase.mdf;Initial Catalog=SIGamepresenterNG;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
The problem is that the database will only attach if I FIRST manually attach and un-attach it. If I don't first do this, it give me a permissions exception.
Is there a fix for this, and if not, how do I work around this?
Update:
Here is the exact error:
Unable to the physical file c:..\mydatabase.mdf. Operating system
error 5(Access is denied). Cannot attach the file 'mydatabase.mdf' as
mydatabase

Related

Connecting to local SQL Server database file

I have a WPF application connecting to a local SQL Server database file (.mdf). I am using Entity Framework.
Connection string is:
Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\db_SmartPOS.mdf;Integrated Security=True
The directory is changed to the following:
string doc = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
AppDomain.CurrentDomain.SetData("DataDirectory", doc + #"\SmartPOS\");
I have copied the .mdf files manually to that specified directory, tested it, and everything is working well on my main machine, where SQL Server 2014 is installed.
Now I am deploying the app to another PC, where I installed SQLLocalDB. However, whenever I try to connect to the database, I get this exception:
System.Data.EntityException. The underlying provider failed to open --> Sysmte.Data.SqlClient.SqlException.....The specified local db instance does not exist.
The .mdf and log files are placed in the directory specified above. I have also tried to copy them to the directory where the application is installed.
I am wondering what am I missing?
So here how it worked.
First of all I found an article online that specified that the connection string in SQL 2014 is different than the one in SQL 2012, therefore Data Source=(LocalDB)\MSSQLLocalDB used in 2014 should be switched to Data Source=(LocalDB)\v11.0. If using Entity framework, this should be done the connection strings settings and in app.config.
Once this was done, i tested it on the other system, and the exception error changed. This time the application was able to connect, however there was something wrong with the versions and i was unable to access it.
I downloaded SQL server 2014 localdb engine and replaced the 2012, and everything worked fine!
Hopefully this will help someone

Error in accessing local db after deploying my application in different pc

I have a C# application which accesses a local auto-named database like DB_Users.mdf to pull out user connection details.
My connection string is:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DB_Users.mdf;Integrated Security=True
When I deploy this application on a laptop, I get an error like this
The database"C:\ProgramFiles(x86)\ISTL\PCArchieSetup|DB_USEr.MDF" can't be opened because itis version 852 ,the Server support version 782 and earlier.Couldn't open new database "C:\programfiles\IstL\PcArchie\DB_users.mdf"Create database is aborted.An attempt to attach an auto-named database for file "C:\programfiles\IstL\PcArchie\DB_users.mdf" failed. A database with the same name exist or specific file cannot be opened on UNC share
Initially, I thought this VS version problem. So I updated to VS community 2015. But still getting the same error. I suspect the file location it's trying to access is the problem. But don't know how to resolve it. Can you share your thoughts on this? Thanks in advance for your help.

service based database run in another PC

I have been working on a project related to database (.mdf). I have created some windows forms in visual studio using C#. Basically these forms work together to store, update and delete data from the Service Based Database i created.when I run this project in another PC there is problem in connection to data base,I change the connection string as in another PC but the smae problem.
SqlConnectionStringBuilder s = new SqlConnectionStringBuilder("Data Source=.\\SQLEXPRESS;AttachDbFilename=H:\\PDF_to_TEXT_PROJECT_MBRM\\PDF-to-Text Convertor\\PDF-to-Text Convertor\\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
can you tell me how solve this problem?
this error:
An attempt to attach an auto-named database for file H:\PDF_to_TEXT_PROJECT_MBRM\PDF-to-Text Convertor\PDF-to-Text Convertor\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

SqlExpress LocalDb can't open file

I have two MDF files in a directory down the AppData/Local path. If I attempt to open them using LocalDb.
My connection string is of the form:
Data
Source=(localdb)\v11.0;AttachDbFilename="C:\Users\Anna\AppData\Local\CaseTrakker
Software\CTDynamoDisconnected\CTDynamoDisconnected_Data.mdf";Integrated
Security=True;Connect Timeout=10
I have a sample desktop application that attempts to connect to this MDF, and I get this exception:
System.Data.SqlClient.SqlException (0x80131904): Cannot open database
"C:\USERS\ANNA\APPDATA\LOCAL\CASETRAKKER
SOFTWARE\CTDYNAMODISCONNECTED\CTDYNAMODISCONNECTED_DATA.MDF" requested
by the login. The login failed. Login failed for user 'IMA\Anna'.
If I move this file to any other location, or rename it (even to a name that is longer), I am able to connect to it.
There appears to be something peculiar about this location or something.
One other odd thing: it worked last week. So far as I am aware, nothing has changed on my machine or my Domain Security.
I'm at a complete loss as to what else to even try. Ideas?
Can you check if there is anything interesting in the LocalDB instance log file? It is located by default in %localappdata%\Microsoft\Microsoft SQL Server Local DB\Instances\v11.0 folder.
One cause of this problem is if you go into your C:\Users\[username] folder and delete the MDF and LDF files. If you do this, then that's akin to doing the same thing to full-blown SQL Server. The server instance still thinks it has the databases but they're obviously not going to work.
A work-around to the problem is to change the database name in your connection string and it should just work.
To actually fix the problem, open up SQL Management Studio, connect to server (LocalDb)\v11.0 (likely with Windows Authentication) and you can detach these databases this way.
In my case I had that DB for a while and mistakenly deleted its MDF and LDF files.
To solve this, I opened SQL Management Studio and connected to (localdb)\MSSQLLocalDB using Windows Authentication then created manually a new empty DB with desired name like that in web.config connection string
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=myDb;Integrated Security=True;" providerName="System.Data.SqlClient" />

Connection to SQL Server is failing from executable

I have a windows Application written at C Sharp, which is updating data store in SQL server 2008. When I am running application from .Net, I have no issues. I compiled application and install on the same laptop. When I run exe file I am getting the following error: "System.Data.SqlException (0x80131904): A network-related or instance-specific error occured while-establishing a connection to SQL Server. The server was not found or was not accessible".
Why I can connect to SQL server from MS Visual Studio, and not able to make connection to SQL from executable file (same laptop) ? Thank you.
Make sure the config file where your SQL connection string should be stored is next to the exe file when you install your application.
Please ensure that you have a file called "projectname.exe.config" right next to your exe. If so, then please ensure that you have the SQL connection string in this config file. If so, the please ensure that it is the correct connection string. Also, please ensure that the user account specified in the connection string has access to the DB. If you are using windows authentication (SSPI) then please ensure the your windows account has access to the DB.
NOTE: the config file name MUST be "exeName.exe.config". The exeName is usually the project name. Just in case you have changed the exe name from the default.
Hope this helps.

Categories