You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor MAUI应用连接SQL Server失败求助

Blazor MAUI连接SQL Server抛出SqlException的排查与解决

1. 捕获完整异常信息

当前仅能看到异常类型,无法定位具体问题,先修改代码捕获详细错误信息,包括SqlException的错误编号和具体描述,这是排查的核心:

using Microsoft.Data.SqlClient;
using System.Diagnostics;

namespace MyApp.Services
{
    public static class SqlServer
    {
        public static void Query()
        {
            Debug.WriteLine("Process started");
            try
            {
                using (SqlConnection connection = new SqlConnection("Data Source=192.168.1.50;Initial Catalog=MYDB;User ID=sa;Password=password;MultipleActiveResultSets=true"))
                {
                    Debug.WriteLine(connection.ConnectionString);
                    connection.Open();
                    Debug.WriteLine("Connection success");
                }
            }
            catch (SqlException ex)
            {
                Debug.WriteLine($"Sql错误编号: {ex.Number}");
                Debug.WriteLine($"错误信息: {ex.Message}");
                if (ex.InnerException != null)
                {
                    Debug.WriteLine($"内部异常: {ex.InnerException.Message}");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"通用异常: {ex.Message}");
                if (ex.InnerException != null)
                {
                    Debug.WriteLine($"内部异常: {ex.InnerException.Message}");
                }
            }
        }
    }
}

根据错误编号可快速定位问题,比如10060代表网络不通,18456代表登录验证失败等。

2. 检查SQL Server远程连接配置

  • 启用TCP/IP协议:打开SQL Server配置管理器,找到「SQL Server网络配置」→ 目标实例的协议,确保TCP/IP处于启用状态,修改后重启SQL Server服务。
  • 开放防火墙端口:数据库所在服务器的防火墙需开放SQL Server默认端口1433(默认实例);如果是命名实例,需开启SQL Server Browser服务,或者在连接字符串中指定端口(格式:Data Source=192.168.1.50,1433;...)。

3. 配置Blazor MAUI平台网络权限

针对移动端平台,需确保应用拥有网络访问权限:

  • Android:在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • iOS:在Info.plist中确认已添加网络权限配置,若数据库使用非加密连接,需允许非安全网络请求(SQL Server用TCP协议,主要确保基础网络权限开启)。

4. 修正连接字符串细节

  • 移除连接字符串末尾多余的分号,避免潜在解析问题:
    原字符串末尾的;可以删除,改为:
    "Data Source=192.168.1.50;Initial Catalog=MYDB;User ID=sa;Password=password;MultipleActiveResultSets=true"
    
  • 再次核对密码的拼写、大小写,确保与Server Explorer中使用的完全一致。

5. 改用异步操作适配Blazor MAUI

Blazor MAUI推荐使用异步方法避免阻塞UI线程,修改代码为异步版本:

public static async Task QueryAsync()
{
    Debug.WriteLine("Process started");
    try
    {
        using (SqlConnection connection = new SqlConnection("Data Source=192.168.1.50;Initial Catalog=MYDB;User ID=sa;Password=password;MultipleActiveResultSets=true"))
        {
            Debug.WriteLine(connection.ConnectionString);
            await connection.OpenAsync();
            Debug.WriteLine("Connection success");
        }
    }
    catch (SqlException ex)
    {
        Debug.WriteLine($"Sql错误编号: {ex.Number}");
        Debug.WriteLine($"错误信息: {ex.Message}");
    }
}

在Razor页面中异步调用:

protected override async Task OnInitializedAsync()
{
    await SqlServer.QueryAsync();
    // 其他初始化逻辑
}

6. 验证SQL Server账号权限

确认sa账号:

  • 已启用登录(在SSMS中查看账号属性→状态→登录为启用);
  • 拥有目标数据库MYDB的访问权限(账号属性→用户映射→勾选MYDB并分配必要权限,如db_datareader)。

内容的提问来源于stack exchange,提问作者Jaden

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 19:53:11