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
Related
I've created an application that works perfectly on my PC using C# and SQL Server 2014.
My connection string is:
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Youssef\Desktop\gone\gone\gone\gestion_incidents.mdf;Initial Catalog=BG_ONE;Integrated Security=True
I know that the connection string is the problem but how can I create a connection string that works on all computers?
Replace your database file path to a generic path that automatically get the location of the project data folder and it will not cause problem when you move project to another location. Follow below steps:
Step 1: First Add App_Data folder in your project and add database file there.
Step 2: Change your connection string replace path before the database file with |DataDirectory|
Example:
<connectionStrings>
<add name="ConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\nwind.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
If you follow the above steps then your connection replace C:\Users\Youssef\Desktop\gone\gone\gone with |DataDirectory|. See modified connection string as below:
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Youssef\Desktop\gone\gone\gone\gestion_incidents.mdf;Initial Catalog=BG_ONE;Integrated Security=True
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>
I want to create an exe of my c# windows application project. I created the exe. But my problem is that I don't know how to include database with this exe. Because now am taking backup of my database and restore this backup to the system in which I want to install my exe. Database is created in sql server2012.
In my c# code connection string set to my system server name. so if I want to install it in another system, I need to change this connection string as server name of the system in which I want to install my exe. But it is not possible in all the time. so is there any method to done all these without changing in the code? I Created the exe using install shield.
Thanks.
Normally database settings should be configurable i.e. the user sets the settings through the application UI which are then written into a configuration file. If you give the settings through a configuration file with hardcoding, the exe need not be built everytime.
For getting the existing database, your application should be coded to create a blank database if the database in the server doesn't exist. The existing data can be imported through Administrator mode od your application or manually done in the SQL Server.
The following code shows how you can store connection strings in App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
</connectionStrings>
</configuration>
Once you have saved your connection string in App.config file you can use System.Configuration.ConfigurationManager class to read this connection string in code.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
ConnectionStringsSettings class provides properties to read connection string settings in your program as following code shows.
string name = conSettings.Name;
string providerName = conSettings.ProviderName;
string connectionString = conSettings.ConnectionString;
The above code has been taken from this link
For a detailed example check this article on CodeProject
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" />
Just trying to publish my first MVC app to Godaddy Plesk hosting.
My app is working just fine running on localhost on my machine but when deploy it to host(used webdeploy ) whenever i try to run some database stuff (used EF -code first) i get: The connection string 'DefaultConnection' in the application's configuration file does not contain the required providerName attribute." error.
Here is my connection string
add name="DefaultConnection" connectionString="Data Source=myserverIP;Network Library =DBMSSOCN;Initial Catalog=mydatabase;User ID=myuser;Password=mypass;" providerName="System.Data.SqlClient"
The strangest thing is that when I use this connection string on my local development machine instead of default "...Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename etc..." string, App runs just fine and I see data stored in database on godaddy hosting.
Also I connect to this database from Microsoft SQL Server Managament Studio using credentials from conncetion string above and without any problem run querys
Hope somebody can help me out
thanks in advance