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

Azure SQL连接正常但SqlDataReader读取异常,VS环境故障求助

问题:Azure SQL连接后SqlDataReader读取异常,本地调试异常但服务器运行正常

我最近碰到个挺棘手的问题,之前一直正常运行的代码突然出状况了,问题疑似出在本地计算机或者Azure SQL服务器上。具体表现是:数据库连接能正常打开,ExecuteNonQuery执行也没问题,但SqlDataReader的读取行为异常。

线上Windows Server虚拟机上的应用程序可执行文件运行完全正常,但本地调试时就出现了问题:

原本正常运行的代码

public static List<ClientDto> GetClientInformation() {
    List<ClientDto> results = new List<ClientDto>();
    try {
        using (var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientData"].ConnectionString)) {
            using (var command = new SqlCommand(null, sqlConnection)) {
                sqlConnection.Open();
                command.CommandType = CommandType.StoredProcedure;
                //running the GetClientInformation stored procedure in DB
                command.CommandText = Constants.GetClientInformation;
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read()) {
                    results.Add(new ClientDto() {
                        Id = Convert.ToInt32(reader["Id"].ToString()),
                        DealerName = reader["DealerName"].ToString(),
                        GroupID = reader["GroupID"].ToString(),
                        DealerId = reader["DealerId"].ToString(),
                        DealerFolderName = reader["DealerFolder"].ToString(),
                        DMSType = reader["DMSType"].ToString(),
                        MAIsActive = Convert.ToBoolean(Convert.ToInt32(reader["MAIsActive"])),
                        SalesIsActive = Convert.ToBoolean(Convert.ToInt32(reader["SalesIsActive"])),
                        SalesSource = reader["SalesSource"].ToString(),
                        InventoryIsActive = Convert.ToBoolean(Convert.ToInt32(reader["InventoryIsActive"])),
                        InventorySource = reader["InventorySource"].ToString(),
                        AppointmentsIsActive = Convert.ToBoolean(Convert.ToInt32(reader["AppointmentsIsActive"])),
                        AppointmentsSource = reader["AppointmentsSource"].ToString(),
                        LeadsIsActive = Convert.ToBoolean(Convert.ToInt32(reader["LeadsIsActive"])),
                        LeadsSource = reader["LeadsSource"].ToString(),
                        ServiceIsActive = Convert.ToBoolean(Convert.ToInt32(reader["ServiceIsActive"])),
                        ServiceSource = reader["ServiceSource"].ToString(),
                        SalesIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["SalesIsDelete"])),
                        SalesDeleteRange = reader["SalesDeleteRange"].ToString(),
                        InventoryIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["InventoryIsDelete"])),
                        InventoryDeleteRange = reader["InventoryDeleteRange"].ToString(),
                        AppointmentsIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["AppointmentsIsDelete"])),
                        AppointmentsDeleteRange = reader["AppointmentsDeleteRange"].ToString(),
                        LeadsIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["LeadsIsDelete"])),
                        LeadsDeleteRange = reader["LeadsDeleteRange"].ToString(),
                        ServiceIsDelete = Convert.ToBoolean(Convert.ToInt32(reader["ServiceIsDelete"])),
                        ServiceDeleteRange = reader["ServiceDeleteRange"].ToString(),
                        IsActive = Convert.ToBoolean(Convert.ToInt32(reader["IsActive"])),
                        UserDefinedName = reader["UserDefinedName"].ToString()
                    });
                }
            }
        }
        return results;
    } catch (Exception ex) {
        //If an exception happens it will insert the error to errorlog table and to the log file
        Logger.WriteError(new ErrorDto() { Source = "GetClientInformation", Message = ex.Message, StackTrace = ex.StackTrace });
        Console.WriteLine(string.Format("Error - {0} - {1} ", "GetClientInformation", ex.Message));
        return results;
    }
}

调试中发现的异常现象

  • 数据库里确实存在数据,但本地调试时,SqlConnection出现内部异常:ServerVersionNormalized = 'sqlConnection.InnerConnection.ServerVersionNormalized' threw an exception of type 'System.NotSupportedException',不过这个异常没有终止代码运行。
  • 调试显示reader存在行数据,但调用reader.Read()枚举时却得不到任何结果。
  • 这个问题在Visual Studio 2015和2017中都出现了,目前我正在尝试重新安装Visual Studio来排查问题。

连接字符串格式

Server=.database.windows.net,1433;Database=; User ID=;Password=;Trusted_Connection=False; Encrypt=True;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:49:37