Unable to find where my database file and connection string is located? - c#

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 :)

Related

Attempt to attach an auto-named database when installing my app in another pc

I recently finished building this C# Winforms app that uses a localDB connection and when installing it on another PC, I get the error as shown in the title, even though I included the SQL Server Express localDB in the prerequisites folder.
I tried all kinds of solutions like changing the method of how to build the setup file (using Advanced Installer), but it seems that the problem is always related to the constant AttachDbFilename attribute of the connection string that's not changing according to where the database is newly installed.
Here's how the connection string is defined in the App.config file:
<connectionStrings>
<add name="client"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Surface\source\repos\VisaTurbo\VisaTurbo\Client.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Please I've been stuck with this problem for 3 days now and the due date to submit the application have already been reached.
Thanks in advance
You can by example check the database file exist in %userProfile%\DBName.mdf and create a new one with https://github.com/martincostello/sqllocaldb.
But this make the connectionstrings parameters obsolete :)
You can also generate a new one with SqlConnectionStringBuilder
But if the user move the database file this will reset the application each time.
That's why in general the database in on a server, not locally stored on the user's machine.

How to change the connection string on another computer?

I am very new to SQL and tried to build a software by using SQLServer and Visual Studio. I created a setup file for my program by using InstallShield Limited Edition Project. I want my program to be used on other computers and I want every user to be able use their own databases installed on their computers. To me, when a user installed the program, the program will search for a connection string that I used while creating the program. Therefore, I think this connection string must be changed by users. How can I add such properties into my program? By the way, I used model first entity framework in my program. My connection string written in app.config is :
<connectionStrings>
<add name="otobusVTNesneleri" connectionString="metadata=res://*/OtobusVeriModeli.csdl|res://*/OtobusVeriModeli.ssdl|res://*/OtobusVeriModeli.msl;provider=System.Data.SqlClient;provider connection string="data source=PIPASO\PIPASOSERVER;initial catalog=otobus;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
P.S. I searched the internet about this problem and saw that people suggest that I use SQL Server compact. I want to solve this problem without using it. I have SQL Server 2012.
You shouldinstall the SQL Server with the same instance name in all the PCs. And then, in the connection string, instead of specifying the computer name, use the "local" syntax:
Data Source=(local)\yourInstanceName
It's not a good idea to let your users change the connection string.
Entity Framework starting from version 6 supports Code Based configuration. Refer to this article: Code-Based Configuration (EF6 onwards)
without use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=Trainee4-PC;Initial Catalog=Demo05-09-2014;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
use sql server username password use it.
<configuration>
<connectionStrings>
<add name="ConString" connectionString="Data Source=DENISH\SA;Initial Catalog=Demo05-09-2014;User ID=Demo;Password=Demo123" />
</connectionStrings>
</configuration>
local databases should be sql compact (sdf files). in any case, what you need to do is to make the installer modify the app.config file after it's deployed. this is what i'm doing in my current project. in my current project i have a local database (an sdf file) and an installer (created using WiX) that deploys my app. i configured the installer to modify some app.config settings after deploying the app. one of the settings was the connection to the local sdf database.
one thing to be careful about is that, while using sdf files, you cannot specify relative file paths in the config file. you need to either specify the full path (ie: d:\..\db.sdf) or use special folder names (ie |AppData|db.sdf). AppData will resolve to the current path of the executable.
requiring a full blown sql database on the client will also mean you'll have to install SQL Express on the client machine. you don't have to do this with sdf files.
in case you decide to use WiX you can modify your connection string like below.
<util:XmlFile Id="WindowsServiceUpdateConnectionString" File="[INSTALLFOLDER]$(var.Phoenix.WindowsService.TargetFileName).config" Action="setValue"
ElementPath= "//configuration/connectionStrings/add[\[]#name="PhoenixCacheEntities"[\]]/#connectionString"
Value="Data Source=[INSTALLFOLDER]Cache\PhoenixLocal.sdf"/>
you can see that an xpath is used here to determine the setting you want to change. hope this helps.
NOT RECOMANDED
its just becouse you said you are just starting in c#
so to write the connection string into a file :
using System.IO;//delcare it the top
File.Write("config.cfg","your connection string goes here");
to read From it :
using System.IO;//delcare it the top
string CNXSTRING = File.Read("config.cfg");
btw you have to create a form where the user and can type the new connection string lets say it have a textbox named txtstring the code will become
File.Write("config.cfg",txtstring.Text);
PS : use the write when you want to save the CNX String like in the save button of that form .

database file not showing in app_data in asp.net mvc4 tutorial using visual studios 2010

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;

using a SQL database in multiple projects

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.

Connection Strings modification after the deployment

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.

Categories