Connection string is not working while using (data source =.\SQLExpress) - c#

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>

Related

Connect to SQL Server in web.config (ASP.NET MVC) using Windows authentication not working, why?

It is under configsection but it's not working and it didn't connect to SQL Server. Can anyone help me with this problem?
Of course, my data source name is . not other thing.
<connectionStrings>
<add name="CompanyContext"
connectionString="Data Source=.;Integrated Security=SSPI;Initial Catalog=Learning_EFCF;"
providerName="System.Data.SqlClient" />
</connectionStrings>

File path to localDb in projekt

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

Problems with connecting locally to SQL Server 2014 Express

I can't connect to my locally DB, it's working fine on the live version. Live version is SQL Server 2012 and test enviroment is SQL Server 2014 Express.
I'm using Web Matrix 3 and a Visual Studio 2013 debugger attached to the process, but I've tried almost everything now? I think the problem is related to connection issues. But i can of course connect to the DB from Management studio.
Here is my connection strings
<connectionStrings>
<add name="ConStrLive" providerName="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=lastbilmagasinet_dk;Persist Security Info=True;User ID=lastbilmagasinet;Password=xxxx;Max Pool Size=10000" />
<add name="ConStrDev" providerName="System.Data.SqlClient" connectionString="Data Source=DVSRV;Initial Catalog=lastbilmagasinet_dk;Persist Security Info=True;User ID=intern;Password=xxxx!;Max Pool Size=10000" />
<add name="ConStrLB" providerName="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=lastbilbasen_dk;Persist Security Info=True;User ID=lastbilbasen;Password=xxxx;Max Pool Size=10000" />
<add name="ConStrLMBanner" providerName="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=lmbanner_dk;Persist Security Info=True;User ID=lmbanner;Password=xxxx;Max Pool Size=10000" />
<add name="ConStrFragtbasen" providerName="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=fragtbasen_dk;Persist Security Info=True;User ID=fragtbasen;Password=xxxx;Max Pool Size=10000" />
<add name="ConStrTransjob" providerName="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=transportjobbasen_dk;Persist Security Info=True;User ID=transportjobbasen;Password=xxxx;Max Pool Size=10000" />
<add name="ConStrLastbilshow" providerName="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=lastbilshow_dk;Persist Security Info=True;User ID=lastbilshow;Password=xxxx;Max Pool Size=10000" />
<add name="ConStrTruckpadborg" providerName="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=truckpadborg_dk;Persist Security Info=True;User ID=truckpadborg;Password=xxxx;Max Pool Size=10000" />
</connectionStrings>
Anybody got any ideas?
The problem was the Datasource. Apparently MSSQL doesn't allow 127.0.0.1 or localhost as local Datasource, or mine which was default configured didn't anyway.
So the datasource in the connection string had to be WIN-BD4MCUAKPRP\SQLEXPRESS.
Then it worked :)

Underlying provider failed to open when I convert database property to do not copy

This is the connection string and when I change the property of the database file (database1.mdf) to "do not copy", it causes an error.
<connectionStrings>
<add name="ClassLibrary1.Properties.Settings.Database1ConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
That's probably because of AttachDbFilename=|DataDirectory|\Database1.mdf that you can't not copy the database to the DataDirectory.
The connection string tries to find and attach the database from the given address and it can't find it. you can attach the db first and then use this connection strings:
<add provider connection string="data source=.;
initial catalog=Database1;
integrated security=True;/>

How do I build the database connection string if the website is on the same server as the db?

I have a problem with my connection string and I couldn't find anything on the web that could help me. So I have a test website on which I connect to a database which is not on the same server. There it all works fine, the data is being read correctly and the connection is successful.
Now when I try the same on a website which is on the same server than the database it doesn't work. So I figured I can't just connect to the database server as I would from a different server if I am already on that server like this:
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=serverName;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
Could anyone help me with that?
If you need more information just ask please.
Thanks in advance!
[edit]
Oh I forgot...this is the error message I get:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: Named
Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Try localhost instead of server name
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=localhost;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=.;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>
Both answers above are correct, however, if you have SQL Express installed (I assume we're talking about MS SQL) then your connection string must be something like this:
<connectionStrings>
<add name="nameOfConnString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=databaseName;User ID=userName;Password=password" providerName="System.Data.SqlClient" />
</connectionStrings>

Categories

Resources