I'm trying to use an 'SQL Express' (or is it SQL Compact?) .MDF file as the datasource on a simple website using a Chart control.
Locally, it works great; but only if the 'AttachDbFilename' has the full path. Am I messing something up, or does it really need to be full path? If so - how do I accomplish this without using something like Server.MapPath()?
<add name="LocalSqlServer"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
By default, the |DataDirectory| variable will be expanded as follow:
For applications placed in a directory on the user machine, this will be the app's (.exe) folder.
For apps running under ClickOnce, this will be a special data folder created by ClickOnce
For Web apps, this will be the App_Data folder
You can programmatically set DataDirectory by calling AppDomain.CurrentDomain.SetData("DataDirectory", newpath)
Related
I have just compiled and installed an application that generates and uses an SQLite database.
In my App.config file, my connection string is like this:
<add name="DBContext" connectionString="data source=.\DB.sqlite" providerName="System.Data.SQLite" />
In my debug/release, I can see that the DB.sqlite file is created in the same folder.
However, when I have installed the compiled application, it goes to a directory called:
C:\Users\uua\AppData\Local\VirtualStore\Program Files (x86)\App
Is there a way to make sure the database appears in a custom location or in the same folder as my installed application?
Is there a way to make sure the database appears in a custom location or in the same folder as my installed application?
You could always use an absolute path:
<add name="DBContext" connectionString="data source=c:\folder\DB.sqlite" providerName="System.Data.SQLite" />
The obvious drawback with this is that you need to make sure that the directory actually exists on the machine(s) where your app is installed.
You may want to consider setting the connection string programmatically.
I'm doing this tutorial on ASP.NET: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4 and I reached a stage in which I need to work with a database.
Since I'm using Windows XP, LocalDB is not an option, so I configured SQL Server Express instead.
The app is working, but I can't seem to see the MovieDBContext.mdf file in the App_Data folder.
I tried refreshing, building, running, nothing helps.
The only thing in that folder is ASPNETDB.mdf. Does this file contain what I'm looking for?
I opened it and it showed a bunch of tables related to users of the app.
My problem comes when it's time to add new fields to the movie class and coordinate it with the database, meaning I have to turn on migration.
Here are the instructions for migration:
http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table
Sadly I cannot delete the database files according to this tutorial since I don't have access to the files.
Is there a way to see the .mdf files?
my connection string is:
<add name="MovieDBContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Drivers;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"
/>
In the order they come in your question
The only thing in that folder is ASPNETDB.mdf. Does this file contain what I'm looking for?
Probably yes, it actually depends on the name you gave to the file when it was created.
Your Connection String
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Movies.mdf;
It looks for a file named Movies.mdf in the App_Data folder whereas your file is names ASPNETDB.mdf, either rename the file to Movies.mdf or change the connection string
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;
i downloaded a demo application from http://code.msdn.microsoft.com/Authorization-based-aa2c0185/view/SourceCode
I run it on my Visual Studio and all worked fine. There is a database ASPNETDB.MDF in its App_Data folder which is used to store the user information or retrieve information for login purpose.
Now what I want is to change this database with another database in my SQL Server.
For that there can be two ways :
Locate the connection string etc. for ASPNETDB and change them with my own database.
or add this ASPNETDB file to SQL Server by attaching. But its showing error while attaching it.
Also if I exclude App_Data folder from project...Then even it works...
Where can find connection string for DB? I want to change the DB with mine own ?
Please Help
Thanks
It's using default connection string from machine.config:
<add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
Go to the Web.debug.config file. There you will find comments noting how to wire up your config file. This will use config transforms so that when you build in debug mode, it will change your config file to override the machine defaults (which is what it is currently using). Just follow the comments and you should be good :)
I have a system comprising 5 applications. Each app accesses a database via a DAL library. In the DAL i have an app.config with the following entry:
<connectionStrings>
<add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Using the full path for the attachDbFilename works fine. But I'm not happy:
I have copied the app.config file into each app that uses the database. Best way of doing this - copying the DAL app.config as a Link in the other projects?
I dont want a full path, when it comes to deployment that aint going to work. Relative paths in the app.config do not appear to work. Ideally I would like to be able to pull the DAL from source control onto any computer and not have to worry about changing the connection string each time. This: http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspx talks about |DataDirectory| for deployment purposes but this doesn't work for me in debug (unless I'm using it wrong, see 3)
This might be better as a separate question but it is related to 2. - Is there a "good" way of arranging multiple projects for debug? I have created a Bin dir and in each project settings I copy the dll/exe to this bin dir. I also have a copy of the database in here (I tried no path in the app.config but that didn't work either, nor did |DataDirectory|). Also incredibly annoying is that relative paths do not work in Debug\Working Directory setting either, so it looks like that is one place that would have to change each time code is checked out to a new machine?
Apologies for the war and peace and thanks in advance for any ideas.
Two answers - but not really full solutions:
1) I have copied the app.config file into each app that uses the database. Best way of doing this - copying the DAL app.config as a Link in the other projects?
You could externalize the connection strings into their own config, something like:
<connectionStrings configSource="connectionStrings.config" />
and then have those connection strings in that new file:
<connectionStrings>
<add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>
That way, you can have custom app.config's, but share the commonality.
2) I don't want a full path, when it comes to deployment that ain't going to work. Relative paths in the app.config do not appear to work.
Unfortunately, the only thing you can do here is to use the |DataDirectory| placeholder, which is a placeholder for the App_Data folder in an ASP.NET application.
<add name="DataAccessLayer.Properties.Settings.ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
The only other solution would be to use a server and connect to a server - rather than having files to dynamically mount and attach.
Using the connection string AttachDbFilename feature from multiple processes referring the same MDF is very bad idea. Sharing an auto-atached database can get you in all sort of complicated security/ownership database startup problems. And specifying User Instance=True is like pouring gas over the flame, since each user instance is per user so if your applications are ever configured to run under different apppool credentials or one suddenly is changed to impersonate, all hell breaks loose.
Just attach for good the MDF as an normal database to your SQL instance and use it as such, with a normall connection string: Data Source=.\SQLEXPRESS;Initial Catalog=<dbname>; Integrated Security=True.
I created the setup project for the application and I can see that the later modifications of the configuration file (Application.exe.config) don't affect the application execution.
I am developing an application with the database file included and I want to enable users to move the database file and modify connection strings.
Does anyone know what's the best practice for the deployment of the application with the database file?
It should work, provided that you use the exact same connection string setting in your DB access DLL's Settings.settings file and in your application's config file.
An example that works well for me:
<connectionStrings>
<add name="YourApp.Properties.Settings.DatabaseConnectionString"
connectionString="Data Source=localhost;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
When entered appropriately in both locations (ie. the dll's Settings.settings and the exe's App.config files), this does allow me to change the database connection in YourApp.exe.config before the app runs.
(I assume you already know that you need to change the application's config file, as DLL's do not support the app.config mechanism directly.)
Have you checked out using a UDL file?
If you save your connection string in the udl file, the user can change the connection via an interface by simply double clicking that file.
You can set your connection string in the app to point to the udl file.
You can also launch the udl interface programmatically if you want.
The only downside to these is if the user saves their password to the file, it is saved as plain text. But this isn't an issue if you are using windows authentication.
did you make sure to remove the settings default values? These are compiled and fetched from the dll and not from the config file.