Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I have created a Maui Blazor app in .NET 7 which is pretty neat and runs beautifully locally. After installing the app on another Windows 10 Pro x64 machine, I get an error message that SQL Server Express (2019) cannot be reached. I get this error message even if I set the connection string, of the target database explicitly in appsettings.json, before publishing the MAUI app. Using a simple Winforms application, I have validated the connection string before publishing.

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: SNI_PN11, error: 26 - Error Locating Server/Instance Specified)


What I have tried:

SQL Server Management Settings:

I have checked the SQL Server Remote setting
I have checked the SQL Port and tried to set it to Default (1433)
Remote connection is switched on
TCP/IP are active
Named Pipe is not present
Firewall rules have already been created for: 135; 1433;1434;4022 & 1434 UDP created (inbound and outbound).
Firewall is switched off
The virus protection is turned off

SQL Profiler at all attempts no event is logged in the trace log of the SQL-Profiler

I have created a small project that illustrates my circumstance. If you are interested, you can have a look at it, compile it and install it on another machine/server.

Unfortunately, I can't upload a sample project here to better explain my starting point.

I'm pretty desperate because I just can't remember where I made a mistake.
Posted
Updated 14-Sep-23 10:04am
v4
Comments
Graeme_Grant 14-Sep-23 17:49pm    
What is the connection string? Please click on "Improve question" and post and replace the user with "user_name" and the password with "password".

1 solution

At a guess, you're trying to connect to .\SQLEXPRESS or something equivalent, which requires that the SQL instance is running on the same machine as your code. In which case, you need to update your connection string to point to the correct server and instance.

If it's already pointing to the correct server and instance, then you need to check that the instance is configured to allow remote connections, and that the firewall on the server is configured to allow connections from the machine where the code is running.
 
Share this answer
 
Comments
dolce_sweet 18-Sep-23 3:08am    
Thank you very much for your help. I have checked all your suggestions and that was not the problem.
I have a link for you which will allow you to download my sample project (https://mycsharp.de/forum/posts/3840302). Search for "MauiGoesEntity.zip". This is from me.

Feel free to criticize the architecture of the sample project, but I don't spend too much time on the architecture of a sample project. It should simply represent in a lean way what my problem is. And in this example, it's about not being able to connect a database to the local SQL Server after a roll-out.
I am already happy to get a hint where I made a mistake / error in thinking, so I can fix it as soon as possible and close my knowledge gap.

Since I have not been working with MAUI for long, I ask for your indulgence.
Richard Deeming 18-Sep-23 3:40am    
Hmmm - download a random Zip file from an unknown third-party site, in the hopes of seeing some code, so that I can dig through it just to tell you exactly the same thing?

No thanks.

There's something wrong with your connection string. That's the only relevant part of your code, and you haven't shown it.
dolce_sweet 18-Sep-23 4:58am    
I can understand your concerns.

The connection string is stored in appsettings.json.
"ConnectionStrings": {
"Default": "Data Source=192.168.99.101\\SQLEXPRESS;Initial Catalog=DemoDatabase;Integrated Security=True;TrustServerCertificate=True;Encrypt=False"
},
First once without user and password. But I have already tried that too.
My sample tests:
- "Data Source=.\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=SimpleUser; Password=SimpleUserPassword; TrustServerCertificate=True; Encrypt=False"
- "Data Source=.\\SQLEXPRESS; Initial Catalog=DemoDatabase; Integrated Security=True"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; Integrated Security=True; TrustServerCertificate=True; Encrypt=False"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=SimpleUser; Password=SimpleUserPassword; TrustServerCertificate=True; Encrypt=False"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; Integrated Security=True"
- "Data Source=DNS-Name\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=sa; Password=sapassword; TrustServerCertificate=True; Encrypt=False"
- "Data Source=IP-Address\\SQLEXPRESS; Initial Catalog=DemoDatabase; User Id=sa; Password=sapassword; TrustServerCertificate=True; Encrypt=False"
- "Server=.\\SQLEXPRESS; Database=DemoDatabase; User Id=SimpleUser; Password=SimpleUserPassword; TrustServerCertificate=True; Encrypt=False"


The json is read into the main MauiProgram.cs class and then the DbContextFactory is registered from it.

var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MauiProgram)).Assembly;

string strAppConfigStreamName = string.Empty;
strAppConfigStreamName = "MauiGoesEntity.appsettings.json";
var stream = assembly.GetManifestResourceStream(strAppConfigStreamName);

var config = new ConfigurationBuilder()
.AddJsonStream(stream)
.Build();

builder.Configuration.AddConfiguration(config);

var appSettings = System.Text.Json.JsonSerializer.Deserialize<appsettings>(assembly.GetManifestResourceStream(strAppConfigStreamName));
var connectionString = appSettings.ConnectionStrings.Default;
builder.Services.AddMauiBlazorWebView();

builder.Services.AddScoped<mauigoesentity.entity.data.orders.iauftraguebersichtservice, mauigoesentity.entity.data.orders.auftraguebersichtservice="">();

builder.Services.AddScoped<mauigoesentity.entity.data.idatabasecontext, mauigoesentity.entity.data.databasecontext="">();
builder.Services.AddDbContextFactory<mauigoesentity.entity.data.databasecontext>((DbContextOptionsBuilder options) =>
options.UseSqlServer(connectionString));
Richard Deeming 18-Sep-23 5:37am    
- "Data Source=.\\SQLEXPRESS;..."
- "Data Source=.\\SQLEXPRESS;..."
- "Server=.\\SQLEXPRESS; ..."

As I said, using .\SQLEXPRESS will try to connect to a SQL Express instance on the same computer where your code is running. Unless you've explicitly installed a SQL Server Express instance on that computer, and a blank copy of your database, that isn't going to work. And if you want your users to use a shared database instance, that would be the wrong thing to do.

So that leaves DNS-Name\SQLEXPRESS and IP-Address\SQLEXPRESS. For the former, you need to check that the computer where your code is running can resolve the DNS name, and it resolves to the correct IP address. And for both, you need to enable remote connections to SQL Express, and configure any and all firewalls between the two computers to allow the traffic through.

I see you've updated your question to say you've already done that, and it's still not working. Well, there's nothing else we can tell you - you must have missed something that's blocking the traffic between the two computers. We don't have access to your network, so we can't tell you what you've missed. You'll need to talk to your network admins to try to diagnose the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900