Clarification on SQL Server connection string - c#

I am using Visual Studio 2013 to write my web app using SQL Server 2012 backend. This is my connection string which works fine.
<connectionStrings>
<add
name="UserDetails"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\UserDetails.mdf;Integrated Security=True;Connection Timeout=30;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
But while deploying as Web deploy package, I could generate .zip and deploy.cmd and setparameters.xml/SourceManifest.xml file gets generated successfully. As guided in deploy-readme.txt, I executed the below command which was successful.
App.deploy.cmd /T /L -allowUntrusted /M:http://localhost/MSDeployAgentService
I tried to replace /T with /Y and here is where I get the error as below.
ERROR
Error Code: ERROR_EXCEPTION_WHILE_CREATING_OBJECT
More Information: Failed to create type 'Microsot.SqlServer.Dac.DacServices'. Learn more at: http://go.microsoft.com/fwlink/?LinkId=221672#ERROR_EXCEPTION_WHILE_CREATING_OBJECT.
Error: Exception has been thrown by the target of an invocation. ---> System.Exception: The connection string argument AttachDBFilename is not supported
AttachDBFilename was proposed in MSDN. I exactly don't understand the reason for this error.
I tried not to include the database while publishing and the above command succeeded and I was able to launch the application hosted. However, SQL Server connection fails at later point. So, I could figure out that I have got some issue with my connection string but I am stuck here for a week. You suggestions will be really helpful and please assist me if I have missed anything.

Related

Entitiy exception was unhandled by user code

I was given a certain project (asp.net project) that was running on a VM. I copied that project to my local machine.(A db is connected to visual studio using entity framework) The sql server that i have has the follow name.
"BASIT-GILANI\MSSQLSERVER1"
and i use windows authentication to login to sql server mgmt studio.
I am continually getting this error
Entitiy exception was unhandled by user code An exception of type
'System.Data.Entity.Core.EntityException' occurred in mscorlib.dll but
was not handled in user code
Additional information: The underlying provider failed on Open.
I am also giving a piece of code from my web.config file
<connectionStrings>
<add name="IntegrationDBEntities" connectionString="metadata=res://*/IntegrationDb.csdl|res://*/IntegrationDb.ssdl|res://*/IntegrationDb.msl;provider=System.Data.SqlClient;provider
connection string="data source=BASIT-GILANI\MSSQLSERVER1;initial catalog=ODSDB;MultipleActiveResultSets=True;App=EntityFramework;Connection Timeout=160"" providerName="System.Data.EntityClient" />
</connectionStrings>
Does anyone have any idea of how to solve this issue?
Do you have sufficient privilege to access the database ?
Have you already try to connect from a different SQL client like SQL Server Management with the same credentials ?

Getting 'connection to SQL server' error while installing a .exe file of my c# project on client's computer

I have created software in c#.net which has some database into it. After creating a .exe file it works fine in my system but after installing that
application in another system it gives following error.
Error: A network-related or instant 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 connection. (provider: SQL Network Interfaces, error: 26 - Error locating server/instance specified).
I have tried installing SQL Server and .net framework in client's machine but still, it's not working.
Do I need to install anything else on the client's pc to run my application?
Check the connection string to the database. The connection string should point to the SQL database the Client has access to. Most of the time this is not the same as the one you are using while developing.
Change connection string of localhost to IP address of Database PC.
You are going to have to add a connectionstring to your App.config file.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=PRANOYPC\SQLEXPRESS; Initial Catalog=DatabaseName; Integrated Security=True;" providerName="System.Data.SqlClient"
</connectionStrings>
If you are using Windows authentication you can leave Integrated Security = True if not you will have to provide the authentication for the Database.
Also you have to make sure that the Client has access to the database that you are trying to access.

Using LocalDB with Visual Studio

I have Visual Studio 2015 installed and I created a sample application from some Microsoft tutorial.
This is the connection string I got in web.config:
<add name="DefaultConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-GeekQuiz-20160203041206.mdf;Initial Catalog=aspnet-GeekQuiz-20160203041206;Integrated Security=True"
providerName="System.Data.SqlClient" />
But it doesn't work, this is the error I got:
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.
Should I install SQL Server Express separately or I got it along with Visual Studio?
I usually use the next trick to set the correct connectionString in the Web.config:
In Visual Studio, go to the “SQL Server Object Explorer” label, right-click on the Database you wish to use and click on “Properties”. Then get the string in the “Connection string” field.
Try giving permissions to SQL server on the directory of App_Data. Go to properties->Security->Edit->Add and add the sql server instance name. It is usually NT Service\MSSQL$InstanceName. You can find instance name from sql server configuration manager-> Under SQL Server Network Configuration. If this doesn't help, then try adding PC Name\Users. It should work after that

Error in update-database command in code first migration

I am working on Desktop application in WPF and creating SqlRepository with LocalDB to store data. I am using following tools
Visual Studio 2013 Community with update 2
Entity Framework 6.1.2 for Code first migration
I have created local database with Microsoft SQL Server Database File (SqlClient) configuration. Database created successfully and below is connection string fetched from Sql
Data Source=(LocalDB)\v11.0;Initial Catalog=D:\USERS\USERNAME\DOCUMENTS\TestDatabase\testdb.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False
I am using same connection string in app.config. I can view database object in SQL Server Database explorer in Visual Studio. But when run update-database command in Nuget package manager console, I am getting below error:
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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
I had similar problem and fixed it when I changed the "start-up project" from another module to the module containing references to all other projects in the solution.
Right-click module >> click "Set as StartUp Project"
Diego's answer is correct.
This problem occurs when there is no connection string in project marked as startup project. Then EF tries to connect to some default database engine to perform update. In my case it tried to use express, and for some reason it couldn't connect. And the error was thrown.
Run your "update-database" with option "-Verbose". One of the lines there shows which StartUp project is used. Check your connection string in this project, or change the startup project to the one that has correct connection string. That solves the problem.
As explained in other answers, the problem usually comes from having the wrong Initial project in Package Manager Console.
In my case the console was ignoring the value I selected in the Default Project drop down list, and also the -StartUpProjectName parameter, and reproducing the wrong behavior of trying to connect to some default database engine, as Mikk's answer describes, in my case using a SqlExpress engine.
My problem was caused by a wrong solution configuration: if your solution has several projects and is meant to be run with the configuration option "Multiple startup projects", but you just downloaded it from your source code control repository, then it is possible that the default configuration option "Single startup project" is being applied to the solution (this config value usually is not checked-in in the source code control). In this case the Package Manager Console just ignores the startup project selected in its combo and just applies the default startup project in the solution, which may not have a connection string, as specified in Mikk's answer.
So I fixed it by changing the solution properties: Common properties / Startup project / Select Multiple startup projects instead of Single startup project, and after that the Package Manager Console would accept the Project name and update the right database.
I was able to solve this by adding parameters to the update-database command:
update-database -StartupProjectName MyApp.Web -ConnectionStringName MyAppConnectStringInWebConfig
I solved this error changing the initial project to the Entity Project.
Look:
Update-Database -Verbose
Using StartUp project 'SCVE.Web'. <-- this ir error
Using NuGet project 'SCVE.EntityFramework'.
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: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
This error message informs you that it is not possible to connect to SQL Server. The possible reasons and their elimination is described below:
1) SQL Server is not started. Starting of it will allow you to see your SQL Server/instance in the drop-down list of available SQL Servers.
Go to the Start menu -> Control Panel -> Administration Tools ->
Services.
In the list of services find SQL Server (instance name, by
default it is EZPARTS5) and check its status, it must be Started
(if it is not started, then right click on SQL Server and select
Start from the context menu).
2) Firewall is blocking port 1433 (MSSQL standard port for connections). It can be disabled following the steps below:
Go to the Start menu -> Control Panel -> Administration Tools ->
Services.
Find Firewall service, it must be disabled (if it is not, then right
click the service and select Stop from the context menu).
Note: More information on this can be found on the official Microsoft site: Link
3) TCP/IP protocol is disabled for MSSQL protocols. To enable it, see the steps below:
Navigate to SQL Server Configuration Manager in the Start menu.
Specify settings for TCP/IP protocol in SQL Server Configuration
Manager.
Restart the computer.
Note: More information on this can be found on the official Microsoft site: Link
4) Make sure your database engine is configured to accept remote connections (If you are using centralized database):
Open SQL Server Management Studio.
Right click SQL Server instance -> Properties -> Connections ->
Check the Allow remote connections to this server box.
Go to the General section and check name of SQL Server specified in
the Name field.
5) If you are using a named SQL Server instance, make sure you are using that instance name in your connection strings. Usually the format needed to specify the database server is machinename\instancename.
6) Make sure your login account has access permission on the database you used during login.
Alternative:
If you still can’t get any connection, you may want to create a SQL account on the server, a corresponding SQL user on the database in question, and just use this username/password login data to connect to SQL Server.
Referred from this link
Try with this Connectionstring:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=D:\USERS\USERNAME\DOCUMENTS\TestDatabase\testdb.MDF;Initial Catalog=testdb;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
If you are trying to connect to Sql Server instead of localdb then make sure the default connection factory in web.config file is as below:
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS2012; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
I had this same problem with a freshly created ASP.NET project.
First I tried to set the startup project as mentioned above. There was no startup project selected and I was unable to select one.
Ultimately I updated my Visual Studio from 2015 Update 1 to 2015 Update 3 and
update-database
executed without problems.
PM>Update-Database
error and solution which I did today in 4 hours.
I am very much new in .net coding, but yes in PHP I have 10+ years experience.
It took me 4 hours to solve this.
1.My first Mistake: I was not having Microsoft SQL Server 2019 in my laptop locally I was having this through AZURE which is not works for localhost, so I installed this locally after download, it took around 2 Hours.
2.Second changed mistake=
"DevConnection": "Server=localhost;Database=PaymentDetailDB;Trusted_Connection=True;MultipleActiveResultSets=True;"
then it works for me

Deployment to Azure from Jenkins not working because of "A network-related or instance-specific error occurred" (Error 26)

A friend of mine setup a Jenkins build server with automatic deployment to Azure. The website used to work fine after deployment, but after we branched our solution and then merged it again (after changing from .aspx to razor) the website displays the following error (locally everything works fine):
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: SQL Network Interfaces, error: 26 -
Error Locating Server/Instance Specified)
Unfortunately we (the two that are left behind) cannot ask our friend how to solve this since he is on a vacation.
After searching on the internet we think it has something to do with our connection string that might be changed. Locally we can connect to the remote Azure database via the "Connect to Database" option in the Visual Studio Server Explorer. When we right click this connection and copy the connection string into our web.config and/or publish profile we still get the same error message on the website.
Our connection string in the web.config looks like this:
<add name="DefaultConnection" connectionString="Data
Source=*******.database.windows.net;Initial Catalog=blablaAszH;Persist
Security Info=True;User ID=testserver#blabla;Password=ourpassword"
providerName="System.Data.SqlClient" />
I do not know what other relevant information I can post but if you ask for it I will place it here.
(The main goal is actually just to deploy the website. But we cannot do this via Visual Studio since only our friend knows the username and password and he's disconnected from the world. These are not the same as in the connection string since when we publish from Visual Studio it says the web deployment task failed because of a wrong username and password. So we need to publish via committing our project to SVN and Jenkins doing the deployment.)
Does anyone here know how to solve this issue or with what it might be related?

Categories