Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Using Messages

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.

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 How to: Create a Visual Basic SMO Project in Visual Studio .NET and How to: Create a Visual C# SMO Project in Visual Studio .NET.

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)


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);
        }

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

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()

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);

        }

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.