sql error not thrown back to caller

只谈情不闲聊 提交于 2019-12-04 11:59:45

You're using a SqlDataReader, it reads only one result set at a time. The exception happens in the second result set. So you have to move the reader to the second result set with NextResult(). Then you hit the exception...

        var connection = new SqlConnection("Data Source=xxx;Initial Catalog=Test;User Id=xxx;Password=xxx");
        connection.Open();

        try
        {
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "TestError";
            command.Parameters.AddWithValue("@ThrowDivideError", 1);
            var reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            reader.Read();
            reader.NextResult();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadLine();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!