从Blazor应用程序运行时,SQL Connection类无法连接 [英] SQL Connection class won't connect when run from a Blazor App

查看:40
本文介绍了从Blazor应用程序运行时,SQL Connection类无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从Blazor网页查询数据库,例外情况如下

I am trying to use the following code to query a database from a Blazor web page, the exception is as follows

建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接.(提供者:SQL网络接口,错误:44-无法为Windows集成身份验证编写服务主体名称(SPN).可能的原因是服务器未正确指定连接API调用,域名系统(DNS)查找失败或内存不足)

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: 44 - Could not compose Service Principal Name (SPN) for Windows Integrated Authentication. Possible causes are server(s) incorrectly specified to connection API calls, Domain Name System (DNS) lookup failure or memory shortage)

System.PlatformNotSupportedException:该平台不支持System.Net.Dns:GetHostByName.

System.PlatformNotSupportedException: System.Net.Dns:GetHostByName is not supported on this platform.

在System.Net.Dns.GetHostByName(System.String主机名)< 0x38c58e8 + 0x0001c>在:0中...

at System.Net.Dns.GetHostByName (System.String hostName) <0x38c58e8 + 0x0001c> in :0 ...

当我在传统的Web或控制台应用程序中运行相同的代码时,连接会立即打开/关闭.

When I run the same code in a traditional web or console app the connection opens/closes instantly.

public class MySQLHelper
{
    public static SqlConnection GetConnection()
    {           
        string m_connectionString = "connection string here";
        SqlConnection sqlConn = new SqlConnection(m_connectionString);
        sqlConn.Open(); // fails to open when called from a regular registered 'service' in Blazor

        return sqlConn;
    }
}

有想法吗?

推荐答案

System.PlatformNotSupportedException:

System.PlatformNotSupportedException:

表示您正在Blazor WebAssembly应用程序中尝试此操作.

Indicates that you are trying this from a Blazor WebAssembly app.

Wasm不支持(不支持)许多API,Blazor限于浏览器安全规则.

A lot of API's are not (cannot be) supported on Wasm, Blazor is limited to the Browser security rules.

您不能直接从WebAssembly(或JavaScript)访问数据库.

You cannot access a database directly from WebAssembly (or JavaScript).

对于客户端计算机上的小型DB,请使用 JS IndexDb (这意味着彼此之间不共享用户)或使用服务器进行数据访问.看一下"Blazor服务器"或"Blazor WebAssembly托管"项目模型.

Either use the JS IndexDb for small DBs on the client computer (that means not shared between users) or use a Server for Data access. Take a look at the "Blazor Server" or the "Blazor WebAssembly Hosted" project models.

这篇关于从Blazor应用程序运行时,SQL Connection类无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆