changing location of App_Data and ASPNET.MDF database - asp.net

I have a bit noobish question, so please forgive me, as I don't have much experience with asp.net.
I have created login control, which uses automatically generated ASPNET.MDF which is located in the App_Data folder.
Connection string looks like this:
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
Now, I would like to place App_Data folder in db folder so database would be in db/App_Data/ASPNET.MDF. How do I change connection string to reflect this.
Thanks you for help

Go into the IIS management of your production machine, and open the IIS, then:
Press on ConnectionStrings (it will give you a list of all of the connection strings in your site)
Alter the connection string of localserver to be similar to:
(data source=.\SQLEXPRESS;Integrated Security=SSPI;DataSource=db/App_Data/ASPNET.MDF;User Instance=true)

Related

how to find the sql connection string in asp.net

I added a new Sql Server Database to my project with the name: ShDB.mdf. It is located in a App_Data folder.
It contains a Table which has some columns. I could add some numbers to the table and then show them in a gridview on my localhost.
This is what I have in the web.config:
`<configuration>
<connectionStrings>
<add name="ShConnectionStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Javad\Desktop\Sharj\App_Data\ShDB.mdf;Integrated Security=True;User Instance=True"/>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ShDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
</system.web>
`
Here is the vb.net code which I used:
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ShConnectionStr").ConnectionString
And it works properly on the localhost test but when I published the website and uploaded the files on a web hosting service to test, I counter an error. How may I fix it? I think I should change my Data source path but I don't know how because its my first experience. Thanks to any help. you can see the error page here: http://www.kaj.somee.com/SL.aspx
Sql Server is either not installed or not running on the web host, or you don't have access to connect to it.
ASP.Net does not by itself know how to use your *.mdf database. It needs a running instance of Sql Server to talk to. It will tell Sql Server to load (attach) your database file, and then send queries to the Sql Server service for execution.
Do you mean you already uploaded the website to a web hosting service, but not the database?
If so, your application may not communicate directly with your database which is actually in your local machine.
You probably need to upload the database to a database hosting service as well (eg.SQL Azure, etc).
You may need to figure out the IP address/Server Name of your database hosting where you have uploaded. These information you can probably get in for the service provider. From there you should be able to connect to the database with the correct configuration.

ASP.NET ConnectionString AttachDbFilename=|DataDirectory|

This is about ConnectionStrings / ASP.NET MVC with Visual Studio 2012 ultimate & SQL Server Express 2012.
Following up with this tutorial here: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4 I came across an issue with these two connection strings at my web.config:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;
Initial Catalog=aspnet-MvcMovie-users;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnet-MvcMovie-users.mdf"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext"
connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|\Movies.mdf;
Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
the website works fine but I couldn't fingure out why the first db is created in the App_Data folder while the second one is created in "C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA"?! I supposed that both will be created in App_data because both utilize this attribute: AttachDBFilename=|DataDirectory|!
note: the tutorial mentions that it should be in the App_Data & they added a screenshot that shows it there indeed!
I have been looking for an answer and got into the complicity of SQL (I thought User Instances might be the solution) but couldn't reach an answer for this : |
(this might be useful to read about User Instances http://msdn.microsoft.com/en-us/library/bb264564(v=sql.90).aspx)
Any ideas are greatly appreciated. Thanks in advance.
Regards
after research/tests it turned out to be as follows:
VS will look at the class name of the DataContext and will look to see if you have provided a connection string with the same name as the class name; for example:
public class MovieDataContext : DbContext
and
<connectionStrings><add name="MovieDataContext" ...
if it manages to find a matching connection string it will create the DB based on the criteria you specified in the respective data string (to add the DB to the App_Data set the path of the DB to |DataDirectory| as shown in both connection strings mentioned in the question); if the name doesn't match or you didn't provide any connection string, VS will fall back to the default settings and will create the DB in the default location/settings (usually C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA).
note neither the "Integrated Security" settings nor the "Initial Catalog" play any role with this (I was able to create the DB in the App_Data with both Integrated Security = True & Integrated Security = SSPI and with/without Initial Catalog).
Hope this helps. Thanks for everyone that participated.
I had the same issue. I believe the difference is in the Integrated Security setting. I have SQLExpress installed and found my Movies database in there using MS SQL Server Management Studio.
Check out this response for a better explanation. Difference between Integrated Security = True and Integrated Security = SSPI
What AMT has given is exactly right. It was confusing as it is to use connection strings with .mdf and .sdf files.
I have another pointer for you though, you can change the default setting where the application looks for a connection string with the name matching the class name of the context class by overriding the constructor of DBContext and providing the paramter nameOrConnectionString as follows
public BlogsContext()
: base("name=EFBlogs")
{
}
Application then searches for a connection string named EFBlogs, if it cannot find connection string then it creates the database with name EFBlogs, instead of BlogsContext
Hi I notice a difference when you add a database and it asks do you want it to be placed in the app_data folder if you click yes then it goes to the app_data folder and the full path name of the mdf is also in the app_data folder whne you use file explorer.

asp.net| change the place webforms save user data

I am trying to change the place my webform saves the data.
What I mean is, when you open a Visual Stuido/C# Web App, its has a user login/register data.
The default data is saved in your project folder, in APP_DATA as: ASPNETDB.MDF.
What about if I want to use MSSQL Server 2008R2.
How can I change it?
Just setup a new database and change your connectionstring in the web.config:
Using SQL Server instead of ASPNETDB.mdf
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
How to: Read Connection Strings from the Web.config File
in really server MS Server 2008R2 you must doing backup from your "home" datafiles: "in APP_DATA as :ASPNETDB.MDF"
p.s. sorry for my bad English.

Where is the database for the built in user part of asp.net MVC2?

User registration/authentication seems to be built into MVC2 but where does is the database that stores the users? It's not under data connections.
The default template uses SQL Express edition. An aspnetdb.mdf file will be stored in the App_Data directory. Look at the connection string in web.config:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
u don't have to use aspnet_regsql.exe tool - just start your app, register new user, and then open the app_data directory in windows explorer - u will find your db file over there
Arek
The database is first created when you create the first user for the website. After that the .mdf file is in app_data folder.
If you want it to be shown in VS, you can click Project -> Show all files and then it should be visible in Solution Explorer. Right click ASPNETDB.MDF and click Include in project.
App_Data folder

My database (.MDF) doesn't seem to attach to my .\SQLExpress but i can view it in VS 2010?

Can anyone help?
I created a DB under APP_Data using ADD ITEM and choosing SQL Server DB and sure enough its there. I can even double click it and it opens up in VS 2010 in the Server Explorer tab.
The connection that i have configured in my web.config is the following
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|testDB.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
As you can see its using the .\SQLExpress. But if i open SQL Server Management and connect to .\SQLEXpress the database is NOT there.
Actually the reason i need to have access to the db is that i need to add membership info into it via aspnet_regsql.exe.
I also tried this via the aspnet_regsql.exe which pops up a gui and i enter the .\SQLExpress in the server name and i choose the db but the db is not there.
I am confused, why is it not attaching it? but VS 2010 can view it in the server explorer tab.
I created a blank aspx file and loaded it via IE and i presummed this would force the attaching of the db.
I must be doing something wrong?
Can anyone provide any info? I know it must be something stupid i am doing
thanks
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|testDB.mdf;User Instance=true
remove User Instance=true
and fyi italics don't work in code blocks :P
And connectionstrings.com is a great resource.
Here's another good link for SQL Express, although it's for 2005

Resources