Connectionstring to other projects' App_Data folder - asp.net

I have a asp.net webforms project with a database in the App_Data folder referenced by connectionstring:
<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
The solution also contains another project with webservices that need to access the same database.
Is it possible to write a relative path to this database in the connnectionstring? - eg. something like this(does not work):
<add name="databaseconnectionstring" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|/../../ProjectName/App_Data/database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

Basically |DataDirectory| is an alias for App_Data folder. Since you are not using App_Data in your services project, it would not help you in this situation.
What you can do though is try starting from the project root using ~\:
AttachDbFilename=~\..\ProjectName\App_Data\database.mdf
Also there is always an option to use an absolute path:
AttachDbFilename=d:\Projects\ProjectName\App_Data\database.mdf
And finally there is always an option to build connection string programmatically.

Related

Whats wrong in this ASP.Net web.config connection string?

i have attached database file C324040_volvo.mdf to my mssql2005 management studio whose name in mssql2005 management studio is ASPNETDB
but page doesn't found the database....and the site doesn't load
i m testing my site in my local computer ...
<connectionStrings>
<add name="connectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\C324040_volvo.mdf;Database=ASPNETDB;Integrated Security=True;User Instance=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
Page searches for a log time ...
If you've already added it to SQL Server then you shouldn't need
AttachDbFilename=C:\C324040_volvo.mdf
If it isn't already attached, then SQL probably doesn't have rights to the root of C: and you should move it.

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

Different connection string for each publish profile in VS2010

Is it possible to change connection string (or just server host) relying on selected web publish profile? Maybe using Web.config transform or someway else?
I mean for profile "Test" change connection string "MyConnString" (in published Web.config) to "Data Source='example.com,14333;..." and for profile "Production" - to "Data Source=./SQLExpress;..."
This is exactly what web config transforms were created for. The link you provided in your post has a walkthrough of doing this specifically for connection strings.
To start with the transforms, right-click your web.config file in the project explorer and choose "Add Config Transforms". Assuming that you have ConfigA and ConfigB in your solution configuration, there will be two new files added, Web.ConfigA.config and Web.ConfigB.config.
If you open these new files, they'll be pretty empty except for a bunch of comments. They actually contain a connection string example in them that you can use though - it looks like this:
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Uncomment this section, and change the "name" property to the name of the connection string in the base web.config file. Set the "connectionString" property to the actual value that you want to use for ConfigA. So, like this:
<connectionStrings>
<add name="myConnectionString"
connectionString="Data Source=ConfigASqlServer;Initial Catalog=ConfigADatabase;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
Repeat the process for the Web.ConfigB.config file, with the desired connection string for ConfigB.
Now when you use the Publish command in visual studio, it will automatically transform the base web.config file, and set the "connectionString" attribute to whatever configuration you're in when you publish.

ASP.NET Sql Provider

I got a problem. When i'm creating the new project in ASP.NET using VS 2010, it's web.config with a default connection string about SQL Express created. But i haven't even got SQL Express installed. What should i do to change the default AspNetSqlProvider to work with my instance of full-weight SQL Server as a services database? And how can i change the template for the ASP.NET project to create a project with my connection?
You mean this one:
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
To change this into one that'll work with SQL Server, it needs to look more like this:
<add name="ApplicationServices"
connectionString="data source=ServerName;Initial Catalog=DatabaseName"
providerName="System.Data.SqlClient" />
where DatabaseName is probably aspnetdb. It'll need some form of authentication, whether you use Windows authentication (in which case you can copy the Integrated Security element from the original connection string) or SQL Server authentication (where you'll have a username/password combination).
There's great info on building connection strings at connectionstrings.com.
To fix this for your future projects, you'll need to change the template for Web Projects. Locate the folder where your C# Web templates are kept (for me this is C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplates\CSharp\Web\1033.
Take a copy of the WebApplicationProject40.zip file (you might also want to backup the original somewhere!).
Inside it you'll find the web.config file with the SQL Express connection string. Change it to a SQL Server string, and then re-assemble the zip file.
The last step is to rebuild the VS template cache - from a command-line (VS Command Prompt probably works best), run devenv /installvstemplates. See here for details.
I just had a similar problem. I had a web site created in VS 2008 and wanted to try web parts. Added some to a page, then started getting:
SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance using a database >location within the applications App_Data directory.
I ran aspnet_regsql.exe (wizard, in .Net framework folder) to create the database and then I added a connection string to web.config:
<configuration>
<connectionStrings>
<add name="aspnet_membership" connectionString="Data Source=localhost; Initial Catalog=aspnetdb; Integrated Security=SSPI;"/>
</connectionStrings>
</configuration>
Then I added this to web.config:
<configuration>
<system.web>
<webParts>
<personalization defaultProvider="SqlPersonalizationProvider">
<providers>
<add name="SqlPersonalizationProvider"
type="System.Web.UI.WebControls.WebParts.SqlPersonalizationProvider"
connectionStringName="aspnet_membership"
applicationName="/" />
</providers>
<authorization>
<deny users="*" verbs="enterSharedScope" />
<allow users="*" verbs="modifyState" />
</authorization>
</personalization>
</webParts>
</system.web>
</configuration>

Relative Path in ASP.NET Web.config

I have the following tag in my ASP.NET Web.config application:
<add name="monitorEntities" connectionString="metadata=res://*/Models.MonitorDB.csdl|res://*/Models.MonitorDB.ssdl|res://*/Models.MonitorDB.msl;provider=System.Data.SQLite;provider connection string='data source="C:\Documents and Settings\jdoe\Desktop\LcdManager\LcdManager\App_Data\monitor.sqlite"'" providerName="System.Data.EntityClient" />
that refers to an EntityFramework instance mapping a sqlite db located in the App_Data directory of the application.
How can i make the link relative so that deploying in a production environment is easier?
Thanks
Have you tried
<add name="monitorEntities" connectionString="metadata=res://*/Models.MonitorDB.csdl|res://*/Models.MonitorDB.ssdl|res://*/Models.MonitorDB.msl;provider=System.Data.SQLite;provider connection string='data source="./App_Data/monitor.sqlite"'" providerName="System.Data.EntityClient" />
or
<add name="monitorEntities" connectionString="metadata=res://*/Models.MonitorDB.csdl|res://*/Models.MonitorDB.ssdl|res://*/Models.MonitorDB.msl;provider=System.Data.SQLite;provider connection string='data source="|DataDirectory|/monitor.sqlite"'" providerName="System.Data.EntityClient" />
Can't you use "|DataDirectory|" in the connection string (instead of C:...\App_Data)? E.g:
connectionString="... |DataDirectory|\monitor.sqlite ..."
I know this works for SQL Server Express databases that are deployed in the app_data folder of the application. But I don't know if this works for Entity Framework / SQLite.
I assume you're talking about this bit
C:\Documents and Settings\jdoe\Desktop\LcdManager\LcdManager\App_Data\monitor.sqlite
instead, try this
../../App_Data/monitor.sqlite
(not sure about the number of '../' but you get the idea
../../App_Data/monitor.sqlite

Resources