Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Gäller för:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
SQL-databas i Förhandsversion av Microsoft Fabric
I hanterad kod utlöses undantag när ett fel inträffar. SMO-metoder och egenskaper rapporterar inte framgång eller fel i returvärdet. Undantag kan i stället fångas och hanteras av en undantagshanterare.
Det finns olika undantagsklasser i SMO. Information om undantaget kan extraheras från undantagsegenskaperna, till exempel egenskapen Meddelande som ger ett textmeddelande om undantaget.
Undantagshanteringsinstruktionerna är specifika för programmeringsspråket. I Microsoft Visual Basic är det till exempel Catch-instruktionen .
Inner Exceptions
Undantag kan antingen vara allmänna eller specifika. Allmänna undantag innehåller en uppsättning specifika undantag. Flera Catch-instruktioner kan användas för att hantera förväntade fel och låta återstående fel omfattas av allmän kod för undantagshantering. Undantag förekommer ofta i en sammanhängande sekvens. Ofta kan ett SMO-undantag ha orsakats av ett SQL-undantag. Sättet att identifiera detta är att använda egenskapen InnerException successivt för att fastställa det ursprungliga undantaget som orsakade det slutliga undantaget på den översta nivån.
Note
SQLException-undantaget deklareras i namnområdet System.Data.SqlClient.
Diagrammet visar flödet av undantag genom programmets lager.
Example
Om du vill använda ett kodexempel som tillhandahålls måste du välja programmeringsmiljö, programmeringsmallen och programmeringsspråket för att skapa ditt program. Mer information finns i Skapa ett Visual C# SMO-projekt i Visual Studio .NET.
Fånga ett undantag i Visual Basic
Det här kodexemplet visar hur du använder try... Fånga... SlutligenVisual Basic-instruktion för att fånga ett SMO-undantag. Alla SMO-undantag har typen SmoException och visas i SMO-referensen. Sekvensen med inre undantag visas för att visa roten för felet. Mer information finns i Visual Basic .NET-dokumentationen.
'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
Fånga ett undantag i Visual C#
Det här kodexemplet visar hur du använder try... Fånga... Slutligen Visual C#-instruktion för att fånga ett SMO-undantag. Alla SMO-undantag har typen SmoException och visas i SMO-referensen. Sekvensen med inre undantag visas för att visa roten för felet. Mer information finns i Visual C#-dokumentationen.
{
//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.");
}
}