SqlExpress LocalDb can't open file - c#

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" />

Related

Getting 'connection to SQL server' error while installing a .exe file of my c# project on client's computer

I have created software in c#.net which has some database into it. After creating a .exe file it works fine in my system but after installing that
application in another system it gives following error.
Error: A network-related or instant 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 connection. (provider: SQL Network Interfaces, error: 26 - Error locating server/instance specified).
I have tried installing SQL Server and .net framework in client's machine but still, it's not working.
Do I need to install anything else on the client's pc to run my application?
Check the connection string to the database. The connection string should point to the SQL database the Client has access to. Most of the time this is not the same as the one you are using while developing.
Change connection string of localhost to IP address of Database PC.
You are going to have to add a connectionstring to your App.config file.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=PRANOYPC\SQLEXPRESS; Initial Catalog=DatabaseName; Integrated Security=True;" providerName="System.Data.SqlClient"
</connectionStrings>
If you are using Windows authentication you can leave Integrated Security = True if not you will have to provide the authentication for the Database.
Also you have to make sure that the Client has access to the database that you are trying to access.

struggling to connect a sql server database

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

Publish .mdf file with desktop app setup using C#

I have developed an application that uses SQL database. My challange is to be able to ship the database with the app itself. When i package my mdf file with the setup, it gets deployed. However it is not able to connect to it.
I have made following as prerequisites
When installing, it downloads and installs all the prerequisites.
I am also using following connection string to connect to it.
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDB.mdf; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
This setup works fine when installed on my machine. Any idea how this will work on simple machines?
Finally I got it working after changing many things. First off, the connection string itself. It was
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDB.mdf; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
But since only SQL Express 2012 is being installed on the destination machine (See the picture in question above) during setup, this was not going to work. So changed it such that it uses SQLServer Express's default instance name
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyFolder\MyDB.mdf; User Instance=True; Integrated Security=SSPI; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
Please note that i have added a folder after |DataDirectory|. Idea is to create a folder within AppData so it would end up creating a folder in C:\users\me\AppData\MyFolder\MyDB.mdf
I also set the User Instance as true because otherwise it wasn't working from AppData folder.
Next I added the code to update the DataDirectory to my desired location, which is in AppData folder as
AppDomain.CurrentDomain.SetData("DataDirectory",Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
I did this change in the main of my program file

How to connect to mdf of local database , which is giving "sql network interfaces error 52"

I have started a new project which i will be displaying the data in the grid view, where i have to consume the .mdf file. Here the problem starts. I am able to open the .mdf file in sql data base, but not by selecting as servertype: "database engine" but by selecting "SQL server compact edition", only then i am able to open the .mdf file in sql studio and create the tables and everything.
I have creted tables and everthing, and now comes the part where i have to give the connection string in the web.config file. I have defined as below.
connstring:
connectionString="Data source=.\SQLEXPRESS\v11.0;Integrated Security=SSPI;AttachDBFilename=C:\Users\usr\Downloads\InterviewSolution\Backup\myproject\App_Data\myproject.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
and i am not able to connect. I have tried to test the connection while configuring the database schema to sqldatasource, but it is failing with below error.
error: "sql network interfaces error 52"
I thought it was permissions issue, and gave full permissions to the file by adding NFS account to it. but still the same, not able to connect through project.
Can anyone pls help me on this, why is the file able to connect to sql studio, but was not able to connect through project.
Is my connection string wrong ??
I always use this template for this case of connection string:
<connectionStrings>
<add name="MyConn"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myproject.mdf;
Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>

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