Using Messages
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics
In SMO, system messages are represented by the SystemMessageCollection object that belongs to the Server object. Because the system messages cannot be modified, SystemMessage object properties are read-only.
User-defined messages are represented programmatically in SMO by the UserDefinedMessageCollection object. Existing user-defined messages can be discovered by iterating through the collection. New user-defined messages can be created by instantiating a new UserDefinedMessage object and setting the appropriate properties.
Examples
For the following code examples, you will have to select the programming environment, programming template and the programming language to create your application. For more information, see Create a Visual C# SMO Project in Visual Studio .NET.
Finding a Particular System Message in Visual Basic
The code example shows how to identify a system message by ID number and display the message.
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference an existing system message using the ItemByIdAndLanguage method.
Dim msg As SystemMessage
msg = srv.SystemMessages.ItemByIdAndLanguage(14126, "us_english")
'Display the message ID and text.
Console.WriteLine(msg.ID.ToString + " " + msg.Text)
Finding a Particular System Message in Visual C#
The code example shows how to identify a system message by ID number and display the message.
{
//Connect to the local, default instance of SQL Server.
Server srv = new Server();
//Reference an existing system message using the
//ItemByIdAndLanguage method.
SystemMessage msg = default(SystemMessage);
msg = srv.SystemMessages.ItemByIdAndLanguage(14126, "us_english");
//Display the message ID and text.
Console.WriteLine(msg.ID.ToString() + " " + msg.Text);
}
Finding a Particular System Message in PowerShell
The code example shows how to identify a system message by ID number and display the message.
# Set the path context to the local, default instance of SQL Server.
CD \sql\localhost\
$srv = get-item default
#Get the message 14126 in US English and display it
$msg = $srv.SystemMessages.ItemByIdAndLanguage(14126, "us_english")
$msg.ID.ToString() + " "+ $msg.Text
Adding a New User-Defined Message in Visual Basic
The code example demonstrates how to create a user-defined message with an ID greater than 50000.
Dim mysrv As Server
mysrv = New Server
Dim udm As UserDefinedMessage
udm = New UserDefinedMessage(mysrv, 50003, "us_english", 16, "Test message")
udm.Create()
Adding a New User-Defined Message in Visual C#
The code example demonstrates how to create a user-defined message with an ID greater than 50000.
{
Server mysrv = new Server();
UserDefinedMessage udm = new UserDefinedMessage(mysrv, 50030, "us_english",16, "Test message");
udm.Create();
UserDefinedMessage msg = mysrv.UserDefinedMessages.ItemByIdAndLanguage(50030, "us_english");
//Display the message ID and text.
Console.WriteLine(msg.ID.ToString() + " " + msg.Text);
}
Adding a New User-Defined Message in PowerShell
The code example demonstrates how to create a user-defined message with an ID greater than 50000.
#Get a server object which corresponds to the default instance
$srv = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server
#Create a new message
$udm = New-Object -TypeName Microsoft.SqlServer.Management.SMO.UserDefinedMessage -argumentlist `
$srv, 50030, "us_english", 16, "Test message"
$udm.Create()
$msg = $srv.UserDefinedMessages.ItemByIdAndLanguage(50030, "us_english");
$msg