Issue connecting to SQL Server database from second project [closed]

zzwlnbp8  于 12个月前  发布在  SQL Server
关注(0)|答案(2)|浏览(134)

Closed. This question does not meet Stack Overflow guidelines . It is not currently accepting answers.

This question does not appear to be about programming within the scope defined in the help center .

Closed 18 days ago.
Improve this question

I have a Blazor Server Side project and a separate API project in my solution. I'm encountering an issue when trying to connect to a SQL Server database from the API project. The connection works fine from the Blazor project, but I get the following error when attempting to establish a connection in the API project:

System.InvalidOperationException: 'Internal connection fatal error.'

Both projects are part of the same solution.

The Blazor project successfully connects to the SQL Server database using the same connection string.

I'm using the System.Data.SqlClient namespace for database connectivity.

The API project runs independently, and other functionalities work as expected.

3okqufwl

3okqufwl1#

It is hard to say definitively, but in scenarios where the main application can connect to an external resource directly but fails to connect when you refactor the logic our to satellite assemblies, then this is often related to how you define the configuration of the connection. In this case I would be looking at the connection string, where is it defined and how is it loaded in your code?

  • The connectionstring loading is specifically not visible in your screenshots.

The debugging sequence I would follow is this:

  1. confirm that the correct connectionstring is loaded before entering the try block
  2. call connection.Open() as the first line inside the try to ensure that you can connect to the database
  3. write the SQL query out to the console or debug output to verify that the query is connect
System.Diagnostics.Debug.WriteLine(query);

This is almost certainly a connection / network issue. The fact that you have suggested the same code works from the main app rules out network issue, so connection string is the most likely culprit.

Step 1 - Confirm connection string
If you think that the connection string is correct, then I would ask how you have verified this. The most definitive way to do this would be at your breakpoint to either write the connection string out to console/debug or to inspect it inside the visual studio debugger.

You shouldn't post connection strings here, but you must first confirm that the connection string that is used matches your main application connection string.

Step 2 - Call connection.Open()
If the connection string is correct, then calling connection.Open() should not throw any errors, if it does then either the connections string is incorrect or there is a network connectivity issue. Most likely is that there is a firewall rule preventing access or the database is not on or configured for remote connections, or you might not be on the same subnet or physical network as the database.

  • You have ruled out all of these issues by stating that the same code, from the same visual studio instance but executed from the main application works.

Ultimately these types of issues are notoriously hard to diagnose in forums like this as we have to trust your observation and debugging abilities. A 1:1 mentoring session where someone can see your code or screen is likely to resolve this in a short number of minutes. Check my bio for hints or use other code mentoring services if you need to resolve this quickly, otherwise please improve your post by including more information and evidence that the connection string and query are correct before they are executed.

When I search the web for -2146233079 This is the first response:

Error Code: 2146233079

The request was aborted: Could not create SSL/TLS secure channel.

That may not be related, but also points to the connection string not being correctly loaded into the runtime.

If your Web API is hosted separately and your blazor runtime is connecting to that over HTTP, then you should double check your appsettings.json or web.config in the web API project or the hosting environment variables... where-ever you have defined the connection string.

The other thing you should be able to do is use tools like Postman or other web API clients to connect to the API in isolation.

The final consideration that can go wrong in your setup is delegated security scopes. Depending on your authentication and authorization flow, the user context executing the query might not have access to the SQL Server resource as your have defined it. Exploring this is well and truly outside of the scope of this post but it might help if you explain your execution stack in a little bit more detail other than a screenshot of the solution explorer in VS.
I have provided this as an answer to short-circuit discussions in comments about how to properly post. But @Medhi you really should be editing your original post to improve the quality of the original question and not posting your own answers or responding directly in comments

oknrviil

oknrviil2#

Upon substituting System.Data.SqlClient with Microsoft.Data.SqlClient, I encountered the System.Globalization.CultureNotFoundException.

In .csproj file of your project, need to add the InvariantGlobalization attribute to false in PropertyGroup.

This is how it looks.

<PropertyGroup>
  <TargetFramework>net8.0</TargetFramework>
  <Nullable>enable</Nullable>
  <ImplicitUsings>enable</ImplicitUsings>
  <InvariantGlobalization>false</InvariantGlobalization>
</PropertyGroup>

enter link description here

相关问题