Aracılığıyla paylaş


SMO İstisnalarını İşleme

Şunlar için geçerlidir:SQL ServerAzure SQL VeritabanıAzure SQL Yönetilen ÖrneğiMicrosoft Fabric'de Azure Synapse AnalyticsSQL veritabanı

Yönetilen kodda bir hata oluştuğunda özel durumlar oluşur. SMO yöntemleri ve özellikleri, dönüş değerinde başarılı veya başarısız olduğunu bildirmez. Bunun yerine, özel durumlar bir özel durum işleyicisi tarafından yakalanabilir ve işlenebilir.

SMO'da farklı özel durum sınıfları vardır. Özel durum hakkındaki bilgiler, özel durum hakkında kısa mesaj veren message özelliği gibi özel durum özelliklerinden ayıklanabilir.

Özel durum işleme deyimleri programlama diline özeldir. Örneğin, Microsoft Visual Basic'te Catch deyimidir.

İç İstisnalar

Özel durumlar genel veya belirli olabilir. Genel özel durumlar belirli özel durumlar kümesini içerir. Beklenen hataları işlemek ve kalan hataların genel özel durum işleme koduna geçmesine izin vermek için çeşitli Catch deyimleri kullanılabilir. Özel durumlar genellikle basamaklı bir dizide oluşur. SMO özel durumunun nedeni genellikle SQL özel durumu olabilir. Bunu algılamanın yolu, son, en üst düzey özel duruma neden olan özgün özel durumu belirlemek için InnerException özelliğini ardışık olarak kullanmaktır.

Note

SQLException özel durumu System.Data.SqlClient ad alanında bildirilir.

Bir excp'nin hangi düzeylerden çıkarıldığını gösteren

Diyagram, uygulamanın katmanları aracılığıyla özel durumların akışını gösterir.

Example

Sağlanan herhangi bir kod örneğini kullanmak için programlama ortamını, programlama şablonunu ve uygulamanızın oluşturulacağı programlama dilini seçmeniz gerekir. Daha fazla bilgi için bkz. Visual Studio .NET'te Visual C# SMO Projesi Oluşturma.

Visual Basic'te Özel Durum Yakalama

Bu kod örneği , Try... Tutmak... Son olarakSMO özel durumunu yakalamak için Visual Basic deyimi. Tüm SMO özel durumları SmoException türüne sahiptir ve SMO başvurusunda listelenir. hatanın kökünü göstermek için iç özel durumların dizisi görüntülenir. Daha fazla bilgi için Visual Basic .NET belgelerine bakın.

'This sample requires the Microsoft.SqlServer.Management.Smo.Agent namespace is included.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Define an Operator object variable by supplying the parent SQL Agent and the name arguments in the constructor.
'Note that the Operator type requires [] parenthesis to differentiate it from a Visual Basic key word.
Dim op As [Operator]
op = New [Operator](srv.JobServer, "Test_Operator")
op.Create()
'Start exception handling.
Try
    'Create the operator again to cause an SMO exception.
    Dim opx As OperatorCategory
    opx = New OperatorCategory(srv.JobServer, "Test_Operator")
    opx.Create()
    'Catch the SMO exception
Catch smoex As SmoException
    Console.WriteLine("This is an SMO Exception")
    'Display the SMO exception message.
    Console.WriteLine(smoex.Message)
    'Display the sequence of non-SMO exceptions that caused the SMO exception.
    Dim ex As Exception
    ex = smoex.InnerException
    Do While ex.InnerException IsNot (Nothing)
        Console.WriteLine(ex.InnerException.Message)
        ex = ex.InnerException
    Loop
    'Catch other non-SMO exceptions.
Catch ex As Exception
    Console.WriteLine("This is not an SMO exception.")
End Try

Visual C'de Özel Durum Yakalama#

Bu kod örneği , Try... Tutmak... Son olarak SMO özel durumunu yakalamak için Visual C# deyimi. Tüm SMO özel durumları SmoException türüne sahiptir ve SMO başvurusunda listelenir. hatanın kökünü göstermek için iç özel durumların dizisi görüntülenir. Daha fazla bilgi için Visual C# belgelerine bakın.

{   
//This sample requires the Microsoft.SqlServer.Management.Smo.Agent namespace to be included.   
//Connect to the local, default instance of SQL Server.   
Server srv;   
srv = new Server();   
//Define an Operator object variable by supplying the parent SQL Agent and the name arguments in the constructor.   
//Note that the Operator type requires [] parenthesis to differentiate it from a Visual Basic key word.   
op = new Operator(srv.JobServer, "Test_Operator");   
op.Create();   
//Start exception handling.   
try {   
    //Create the operator again to cause an SMO exception.   
    OperatorCategory opx;   
    opx = new OperatorCategory(srv.JobServer, "Test_Operator");   
    opx.Create();   
}   
//Catch the SMO exception   
catch (SmoException smoex) {   
    Console.WriteLine("This is an SMO Exception");   
   //Display the SMO exception message.   
   Console.WriteLine(smoex.Message);   
   //Display the sequence of non-SMO exceptions that caused the SMO exception.   
   Exception ex;   
   ex = smoex.InnerException;   
   while (!object.ReferenceEquals(ex.InnerException, (null))) {   
      Console.WriteLine(ex.InnerException.Message);   
      ex = ex.InnerException;   
    }   
    }   
   //Catch other non-SMO exceptions.   
   catch (Exception ex) {   
      Console.WriteLine("This is not an SMO exception.");   
}   
}