error while Connecting .mdf database - c#

I developed a small project in visual studio 2010. In my project I attach a service-based database named database1.mdf.
My connectionString is :
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True
It works fine on my developer pc but it throws an exception on a client's pc.
Exception is :
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 don't understand what is happening.

Two things need to occur before you can connect to a SQL Server Express database.
SQL Server Express must be installed on the target server. Having the file present isn't sufficient. (This is what the error you're experiencing likely means).
The path to your database file should be an absolute path to rule out the possibility of a file location error.

this connection string is for your developer machine. you share your database on the network. you also must update your connection string by adding ip address of your server.

Related

C#, Visual Studio 2022 update, error after publishing - provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified

There are a lot of similar questions here and I think I read all of them, but still haven't found the cause of this error:
provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified
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
The code:
string connection = Properties.Settings.Default.SqlConnection; 
SqlConnectionStringBuilder builder = new() 
{ 
["Server"] = connection, 
["Database"] = "myDatabase", 
["Trusted_Connection"] = true 
}; 
using SqlConnection conn = new(builder.ConnectionString); 
try 
{ 
conn.Open(); 
} 
catch (Exception ex) 
{ 
throw;
} 
finally 
{ 
conn.Close(); 
} 
I store myServerName\myInstanceName in the settings:
<add name="TestSQLConnection.Properties.Settings.SqlConnection" 
connectionString="Servername\SQL2012" /> 
This works on my local machine, but when I publish the application, the user will get the above mentioned error. If I log in and run the published application on the server, it works (but I am admin, so that is not a surprise) and strangely enough, after I run it, other users will be able to run it without error, but only temporarily (How is this possible?). I am having this issue since the latest update of VS2022, before that it worked and if I publish an older project now, the user gets the same error. All the applications I published earlier with the same connection string work just fine (which rules out most of the possible causes discussed here in earlier questions).
Things that I tried:
Make sure there is no typo in the connection string
Make sure that the server is reachable
Replace server name with IP in the connection string
Check if SQL Server Browser is running
Enable port 1433 in Server Configuration Manager
Add port 1433 to the connection string
Test applications published earlier with similar code (same connection string) and the same user.
Repair Visual Studio 2022
Any idea where should I try to look for the root cause would be highly appreciated. Can this be an issue with VS (I also tried to repair VS out of desperation, with no avail) or am I missing something trivial?
UPDATE
It is definitely an issue with the access rights. If I publish the application to a folder that is already in use for an application deployed before, it works. The funny thing is that there is no difference in security settings between the old and new folders. (same users/groups have the same access rights to the folders)

C# app with SqlClient does not work on another computer

I have written an application that uses SqlClient and a local mdf database. However, it does not work on another machine (throwing an exception that the server cannot be run) and I assume this is because there is no SQL server installed? But I thought that using local database and SqlClient elimites the need for the server, something like the old SQL CE. Is that the problem?
EDIT Exception: System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing 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
As for connection string, I keep the one generated by Visual Studio Express, just modify the path to use the app folder.
SqlConnection s = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename ="+ System.IO.Path.GetDirectoryName(Application.ExecutablePath)+ #"\Database1.mdf; Integrated Security = True");
Is there a simple way how to rework this so that my app does not require SQL server installed? All I need is to have it use the local database file in its folder.

Hosting ASP.net app on somee - database update from C# app

I have ASP.NET app with database which is updated by C# application, so database is placed on my local disc. .NET app can only read entries (changes are not allowed).
I can run my .NET app from local host (from Visual Studio or/and IIS) or other computer in network by ipaddress:port_number/page_name.aspx. Everything is working fine.
But now I would like to publish my app on somee. :) I made account and in the same project I added one simple page (just plain text) and that page can be accessed my other computers fron Internet by typing myusername.somee.com/that_simple_page_name.aspx
I found a lot of similar topics on the Internet forums so I made changes:
- TCP/IP enabled in SQL Server Configuration Manager
- exceptions ports 1433 and 1434 (UDP),
- SQL Server Browser enabled...
I made .zip of folder created during IIS publishing (c:\inetpub\wwwroot\My_App_name) and uploaded it to the some by File Manager Upload. But when I tried to access to the page with readings from database I got error
Server Error in '/' 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
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) Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and
where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: 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 believe that problem is with my connection string. My current connection string (web config file uploaded to the somee and on my Visual Studio 2013 Ultimate) is:
<connectionStrings>
<add name="_web_server_test.Properties.Settings.Base1ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Database Somee web\Base1.mdf";Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
Can you recognize what is the problem and how to change connection string?
Thanks.
You first have to create the database manually in your dashboard.After creating a database the page will show you the connection string you can use in your webconfig file.
make sure that you pick the right SQL version on the somee dropdown, the latest version is what worked best for me.

Can't connect to SQL Server in visual studio 2012

I am trying to connect to an SQL server database in visual studio 2012 but having no luck. I have the following code.
var db = Database.Open("anagram_database");
var shows_data = db.Query("SELECT * FROM sorted_words");
the database name is correct without any typo's, I am receiving 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: 26 - Error Locating Server/Instance Specified)
I am completely out of my depth with this and don't even know where to start. I cant seem to find anything with googling.
From the documentation:
name
Type: System.String
The name associated with the database to open. name can specify an .sdf or .mdf database file that is in the App_Data folder. (Do not include the file-name extension.) Alternatively, name can specify the name of a connection string in the Web.config file.
Here, App_Data means the logged in user's Application Data folder (or actually, the user running the application, which is probably the same). Type %APPDATA% in the address field of a Windows Explorer and press enter. The folder that opens is where you should place the anagram_database.mdf database file.

New ASP.net MVC Project - Log On not working

Consider a new ASP.NET MVC project and haven't done any modifications to the code. I run the application and click the [Log On] link at the top right and try to register a new account. After about a minute it times out with the following:
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 am running on Windows Server 2008 R2.
Do you have a local instance of SQL server express installed and running?
Look at your web.config <connectionStrings> element. Within there, you'll see a connection string. Find the server or data source portion, and ensure that you have a server with that name.
It defaults to connectionString="data source=.\SQLEXPRESS. Ensure you tweak that as per your environment.

Categories

Resources