What path should be given to the base file in the project.
I tried it but it does not work:
` <connectionStrings>
<add name="connection" connectionString="Data Source=(LocalDb)\v11.0; AttachDbFilename=..\Database.mdf;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>`
You can use |DataDirectory| to get the base of your project.
AttachDbFilename=|DataDirectory|\Database.mdf
Related
I'm using ASP.NET with C# and SQL Server
T have to use my .mdf file inside the App_Data folder because I don't have access to the SQL database.
T have a problem with this connection string only if T use .\SQLExpress
<connectionStrings>
<add name="msscEduConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MsscEdu.mdf;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
This connection string is not working and it shows error with attaching database
We have another site with connection string like this but different name and it works fine.
This is the connection string for the other site
<connectionStrings>
<add name="msdschoolkjConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\msdschoolkj.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
If I use the following connection string it works only on my computer but not the server
<connectionStrings>
<add name="msscEduConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MsscEdu.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Can anyone assist?
Try this in your web.config file
<connectionStrings>
<add name="ConnectionName"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
In keeping with the recommendation here we separated our connection string into an xml file. Our web.config:
//old - works
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<connectionStrings>
//new - fails
<connectionStrings configSource="connections.config" />
content of connections.config:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Both files are located in the project root folder. EntityFramework works fine with both solutions. However when I try to obtain the connection string for some tests it fails to retrieve.This is the code that we use to extract the connection string:
string ConnectionString =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
Reading the connection string direct from web.config works, when we introduce the ConfigSource it fails. What do we need to make it work
You need to set the Copy to Output Directory property of your configuration file to either Copy always or Copy if newer.
If you don't do this, your ConnectionStrings.config file will not be copied to the output debug/release folder and so will not be found.
Also note that the external file should have the same name as of the section name. Like it should be connectionStrings.config, not connections.config
I have my web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SRV;Initial Catalog=SomeCatalog;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I have my web.debug.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SRV;Initial Catalog=SomeCatalog;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(DefaultConnection)"/>
</connectionStrings>
I have my web.defaultserver.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\;Initial Catalog=EverybodyPilates;Integrated Security=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(DefaultConnection)" />
</connectionStrings>
When I run the migrations:
> Update-Database -Force -Verbose
It hangs for ages because it can't seem to find my connection string. Am I doing my web config correctly?
Or have I got something else incorrect?
It's interesting that it hangs rather than returning an error. By default it will pull connection information from the startup project. You can specify the projects manually:
Update-Database –SourceMigration $InitialDatabase -ProjectName ProjectWithMigrations -StartUpProjectName WebApp
You can also force your context to use that connection string, by default it will look for one with based on the context name.
public class MyContext : DbContext
{
static MyContext()
{
}
public MyContext()
: base("Name=DefaultConnection")
{
}
}
If none of that seems to help you might try right clicking on your web app and choosing publish, and do a simple file system publish in Debug build configuration and manually inspect the web.config to see if it transformed properly and even throw it in wwwroot, if there are connection string issues you should get an exception.
i have developed a project. i am uploading this project in a server. i could not connect this with the database.
what will be thw path in web.config file. help me please.this is my connection string in web.config file.
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"/>
</connectionStrings>
should i use database path there?
D:\microsoft sql server\data\bcharya.mdf
this path or
D:\microsoft sql server\data\bcharya_log.LDF
MDF Location
Put your .MDF file in App_data folder.
Connection string
<connectionStrings>
<add name="bcharyaConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bcharya.mdf;
Integrated Security=True;
User Id=myUsername;
Password=myPassword;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Add a backslash "\" just after |DataDirectory| in your connection string:
AttachDBFilename=|DataDirectory|\bcharya.mdf;
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\bcharya.mdf;User Instance=true"/>
</connectionStrings>
|DataDirectory| ( Enclosed in pipe symbols ) is a substitution string that indicates the path to the database.
To set the DataDirectory property, call the AppDomain.SetData method. If you do not set the DataDirectory property, the following default rules will be applied to access the database folder:
1: For applications that are put in a folder on the user's computer, the database folder uses the application folder.
2: For web application the database folder uses App_Data folder.
You can set the path in Application_Start method in your Global.ascx.cs
AppDomain.CurrentDomain.SetData("DataDirectory", #"D:\microsoft sql server\data");
DO NOT Forget to change
AttachDBFilename=|DataDirectory|\aspnetdb.mdf;
to
AttachDBFilename=|DataDirectory|\bcharya.mdf;
If the database is already attached to sql server change the connection to:
<connectionStrings>
<add name="bcharyaConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog = bcharya;User Instance=true"/>
</connectionStrings>
This way you don't need to set the |DataDirectory| path.
I am working on my 1st mvc razor project. I set the project and on the 1st run I am getting an error that the _appstart.cshtml page can't connect to the database.
I can see the db in the app_data folder and on this line of code I am getting error:
WebSecurity.InitializeDatabaseConnection("StarterSite", "UserProfile", "UserId", "Email", true);
This is my 1st time working on mvc razor project, and every advice is welcome about what should I enter there.
UPDATE:
conn string in web.config:
<connectionStrings>
<add name="StarterSite" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\StarterSite.mdf;User instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Make sure your connection string is pointing to the correct location (database). You can find it in your Web.config file in your solution.
e.g.
<connectionStrings>
<add name="WebContext" connectionString="Data Source=server.local;Initial Catalog=blops.test;Persist Security Info=True;User ID=Userid;Password=password" providerName="System.Data.SqlClient"/>
</connectionStrings>