In this SQL Server tutorial, I want to share with developers how they can create a VB.NET project in Visual Studio 2015 referencing SQL Server Managemet Objects (SMO) libraries.
As a second step, using a VB.NET Windows GUI we will be able to connect to a SQL Server instance and list all databases on the form.
The last step will be selecting a SQL database to script all tables existing in that database and saving SQL script file using a File Save dialog control.

I hope database developers find this SQL Server SMO tutorial useful because generating database table scripts is a frequent task for many SQL Server administrator and developers managed for various reasons.

Next step is choosing the right step for our application for SMO tasks.
You can develop your project in VB.NET as I did in this tutorial or develop in Visual C#
To display response visually for the sake of this tutorial, I preferred a Windows Form application.
But for automated task through SQL Server Management Objects SMO libraries, a console application could be also preferred.

I completed development project template selection as seen in following screenshot.
In this SQL tutorial, we will be building a Windows Forms application in Visual Basic using SQL Server Management Objects (SMO)

In our SMO project, we need to add SMO libraries (SQL Server Management Objects) as project reference.
Programmers require each of following SMO assemblies to add as reference to their SQL Server project:Microsoft.SqlServer.SqlEnum.dllMicrosoft.SqlServer.Smo.dllMicrosoft.SqlServer.Management.Sdk.Sfc.dllMicrosoft.SqlServer.ConnectionInfo.dll

Click Browse button on Visual Studio Reference Manager dialog screen to point and select SMO libraries from where are installed.

Point to: C:\Program Files\Microsoft SQL Server\120\SDK\Assemblies for SQL Server 2014 assemblies.
If you have other SQL Server versions installed on your server, you can browse the installation path for corresponding version including SDK and Assemblies folder.

Windows Form Design for SQL Server Management Objects Project

The form design of the final product is important. For the simplicity of this tutorial, I preferred to enable the user to provide a SQL Server instance name manually to connect and list databases created on that SQL Server installation.
When the user selects one of the databases, using a button database administrator or SQL programmer will be able to generate create table scripts of selected database and save them locally.

Maybe it is not created through Design Thinking principles but I believe below Windows Form application design is a good starting point for our requirements.

SQL Server Management Objects scripting task project GUI design

On Form1.vb Windows form in design mode, programmers can add UI controls from Visual Studio toolbox as seen in below screenshot.

Label with name as Label1 and text as ServerTextBox with name as txtServer set to empty string in design mode.Button named btnConnectServer with text List Databases to connect to target SQL Server instanceListBox named listDatabases for database list for selected SQL ServerButton named btnScriptDatabase and text Script TablesButton named btnExit displaying text Exit for closing the app.
Finally, add a SaveFileDialog control with name SaveFileDialog1 on to the form design layout.

.NET Developers are now ready to switch to code editor on Visual Studio IDE. A double click on the form layout will let programmer to Code Editor view in Visual Studio.

If you have named controls added on the layout with exactly the same names as I give above, when the Form1.vb code editor is displayed, copy and paste below code by replacing all existing code on the form.
If the control names are different, you will have to refactor below code for correct names before building your project.

Dim obj = New Urn(0) {}
For Each tbl As Table In db.Tables
obj(0) = tbl.Urn
If tbl.IsSystemObject = False Then
Dim sc As StringCollection = scrpt.Script(obj)
For Each st In sc
sb.Append(st)
sb.Append(vbNewLine)
Next
End If
Next

If .NET programmers look at the code carefully, they will realize at the top of the form we have imported referenced libraries. So that developer can refer SMO objects or SQL Server Management Objects in the Visual Basic code.

Additionally we need System.IO and System.Text to download and save all table scripts as a single file on selected target path.

Using SMO, it is possible to connect to a SQL Server instance by using Server object.New Server() by providing the instance name will create an object reference to the SQL Server database instance which is ready to connect by default with Windows authentication.

When application is loaded for first time, I automatically display "localhost" in server instance textbox.
The user is free to change the SQL Server instance name.
Pressing the btnConnectServer button creates a Server object for related SQL Server instance using SMO libraries.

In the same event handler, I use a For Each loop which loops through each database in the target SQL Server instance.
Every database name is added to the listbox control.

The database collection, or list of all databases can be reached using Server.Databases collection.

When the user choose a database name from the all available databases list in listbox and then clicks btnScriptDatabase button, click event is triggered.

To create scripts in SQL Server Management Objects (SMO), Scripter object is used. Scripter is created by passing the SQL Server instance as an input argument.

For creating script for a specific database object, Scripter.Script method is used in SMO.
By passing the database object, in our tutorial we pass each table object one by one to Scripter object Script method through a For Each loop on db.Tables collection for selected database.

The last section in our sample SQL Server SMO codes is related with SaveFileDialog control which is used to save all scripts as a text file (.sql)
Programmers can use File.WriteAllText method to save Scripter object Script method outputs which are stored as string using a StringBuilder instance.

Finally, developers are ready to Build project in Visual Studio IDE and test their SQL Server application which will connect to a SQL Server instance, list all databases for the user to select one among them and enable the user to get Create Table scripts for all existing tables in target database.

In Visual Studio, using top menu follow Build > Rebuild Solution for building your SQL Server SMO project.
By default project will be build in debug mode. After you complete all development task, before deploying your solution to productive environmen, build it in release mode and then deploy it.