Publish application and include .dbo and tables? - c#

I am trying to publish my Windows forms application which uses a data connection (.dbo) for tables that run my application. However, when I publish the application, I get errors saying the application cannot access the SQL database.
I have looked through YouTube videos and read on the Windows website but I cannot really find anything that solves my problem. The server is a local server named after my laptop name. All of them talk about .mdf databases but I can only see a .dbo under data connections which includes all the tables I created for my application.
I get the following error:
"Unhandled exception has occurred in your application...
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 the instance name is correct and that SQL Server is configured to allow remote connection. (provider: Named Pipes Provider. error: 40 - Could not open a connection to SQL Server)"
It works fine when I run the .exe file on my computer where I built the application but the above error is given when I run the application on another computer.
Can anybody help me publish my application to include the .dbo and all tables?

Related

ASP.NET Core Web 3.1 - IIS Express can fetch data from localdb but local IIS can not

I have a very weird problem as stated above.
The code running is exactly the same, the only difference is the Debug profile used. I'm using Microsoft Visual Studio Community 2019 Version 16.6.3.
When using the IIS profile the code says I have no Media in my database.
Switching profile to IIS Express, now I have Media using the exakt same connection string as can be seen in Immediate Windows.
I first suspected that the ASP.NET Core Web 3.1 had a problem fetching the connection string from appsettings.json and that it was needed in web.config as well. Tried adding it but it did not make a difference. Checking context.Database.GetDbConnection().ConnectionString also shows the same for both profiles so this is not the case.
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=(localdb)\\mssqllocaldb;Database=<MYDB>;Trusted_Connection=True;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
</connectionStrings>
If I look at the database via SSMS I can see my Media as expected.
The IIS Application Pool runs as LocalSystem. If I switch to a user that does not have access to the database like ApplicationPoolIdentity I get an exception as expected in Windows Event Viewer and the application does not start correctly.
Microsoft.Data.SqlClient.SqlException (0x80131904): 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: 50
Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
Using LocalSystem everything looks fine in Event Viewer but it says I have no Media or any other data.
How can two identical connection strings using the same code show different results?
Tried switching Application Pool Identity to my currently logged in user that I know has access via SSMS:
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: 50 - Local Database Runtime error occurred.
Error occurred during LocalDB instance startup: SQL Server process
failed to start. )
With this and the comment from #nilsK I fixed it like this:
Start by using Developer PowerShell or Developer Command Prompt for Visual Studio as Administrator and confirm that you only have one instance of LocalDb.
Type sqllocaldb info to see your LocalDb instances.
I then followed this article and had Application Pool Identity still set to my currently logged in user, credit: https://stackoverflow.com/a/38294458/3850405
https://learn.microsoft.com/en-us/archive/blogs/sqlexpress/using-localdb-with-full-iis-part-1-user-profile
In there I found that it is not enough to have Load User Profile set to true for your Application Pool, you also need to set setProfileEnvironment to true in applicationHost.config normally located at C:\Windows\System32\inetsrv\config. With this configuration it worked:

SQL Server Reporting Services LocalDB doesn't exist error

I've been trying to view deployed reports in SSRS using Visual Studio 2015 and SQL Server 2014, but this is the error message I've been receiving:
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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist. )
This is what my connection string looks like:
Data Source=(localdb)\VIRAJ-PC;Initial Catalog=AdventureWorks2012
I've done numerous things to troubleshoot this problem including editing the firewall settings to open ports 1433 and 1434 that need to be accessed, made sure that the MSSQLSERVER service was running, configured the local server to allow incoming connections, have turned on TCP/IP and Named Pipes protocols through the configuration manager, and have checked to see if the LocalDB instance is running through sqllocaldb.exe in the command line (it is running).
If it matters at all, I'm using SQL Server Management Studio 2016. I don't want to have to do an uninstall as I've done so two or three times already because of previous errors and have gone through a lot of configuration so if there's a way to fix this without having to just reinstall, I would appreciate it.

Unable to publish ASP.NET database to external server

I'm trying to publish my ASP.NET MVC application to my server running Windows Server 2008.
I have installed IIS
I have created a database in on the (LocalDb)\v11.0 server on the destination server (The DB is named TestPublishWebApp).
I've clicked publish and have set the connection (and validated the connection):
And I have set the settings as well:
However whenever I publish the application I and go to the URL I get the following 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: 50 - Local Database Runtime error occurred.
Cannot create an automatic instance. See the Windows Application event
log for error details.
This is the very first time I'm trying to publish an application to an external server. What could I be doing wrong?
And yes, I have verified that the SQL server is running.

permission and timeout issues connecting to local northwind database in C# entity data model

i am trying to connect to the northwind database in a C# project (entity data model). i am running on my own machine as admin, and have permissions both as admin and as user Gary\PC-Gary
In the Connection Properties dialog box , when i locate the northwind datbase, and double click it
i get the following error message:
Northwind
you don't have permission to open this file.
Contact the file owner or an administrator to obtain permission
i detach the database from SQL Server (on the understanding that C# 2010 will not allow a user instance to connect to a database that is currently in use)
I seem to be making progress, because now, when I browse to the database file, i seem to get past the connection issues, and CAN pull up the filename.
HOWEVER, when i attempt to test the connection (or create the EDMX model), i get the following error message. i am using windows authentication.
a network-related or instance-specific error occured 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 have verified that the instance is running in SQL Server Configuration manager and in SSMS. does sql server have to be configured to allow remote connections to access a database on your own machine?? how would this be done?
Found a suggestion here by Peja Tao. Maybe that will help.
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/8cdc71eb-6929-4ae8-a5a8-c1f461bd61b4/provider-sql-network-interfaces-error-26-error-locating-serverinstance-specified

"A network-related or instance-specific error occurred.." on Production Server Deployment

This is probably something simple but i just cannot figure it out so i'm just going to put it out there. In my c# class library i have a connection string
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\As.mdf;Integrated Security=True;User Instance=True
And this works fine in my local development environment. However this class library needs to be consumed by a web component and thus i need to upload it to my ISP. When i try this in my local IIS, i have to put the database files in the App_Data folder of my web service app and it works great. When i upload it the ISP server (and put the database files in the App_Data folder) i get the 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.
What have i got wrong here? Please help.
Is MS SQL Server Express available at the ISP server? Is there an instance running on the server that is named SQLEXPRESS?
I doubt that they are running a SQL Server Express instance on the web server.

Categories