The MS SQL Integration Plugin brings the power of .Net in Unreal Engine. With this plugin you can store and retrieve data and images from your SQL Server. Build your own queries and select and update data to your database directly from Blueprints. No restrictions in query formation. Any query working inside SQL Client , works here, which means you can select and update multiple data at once , use joins and sub-queries, as well as create and drop tables via Blueprints.
Asynchronously connect your Server via Windows Authentication System as well as SQL Server Authentication System.
Form your own query and pass as string input , directly within your Blueprint or C++ class.
Easily Save and Retrieve Images from your Database
Create custom connection string format with different protocols and other connection parameters, as required by your server.
If you have worked with MS SQL Server, you must be very familiar with Select, Update, Insert and Delete SQL queries, joins and sub-queries. If you know C#.Net, and worked with ADO.Net, you must be knowing how to set Connection properties for your SQL Server, and how to write and execute your SQL Queries within your .NET application.
But now you can use Blueprints in Unreal Engine 4 to do your job. You are still writing the same queries that you are familiar with, but here you are writing it within Blueprints, which gives you flexibility to store and retrieve data as well as Images from your SQL Server to your UE4 projects.
To get started, first ensure that the plugin is installed, by visiting the Plugin Window, as shown below:
If you are not familiar with SQL Server at all, you will get plenty of online resources to get you started, and you can definitely test it out by downloading the free version, SQL Server Express. SQL Server Express is an entry level database server having a limitation of 10 GB size, which is perfect from small scale applications. Here I will be explaining you in details how to set up your database connection and how to write and execute SQL queries from UE4.
Assuming that you have your database server up and running, either locally or remotely, to get started working with this plugin, you first need to create a database and a table in your server, where you would like to store all your in-game data. If you are unsure regarding how to work with SQL Express, let me quickly walk you through the basic steps.
Download and install SQL Server Express edition from here : https://go.microsoft.com/fwlink/?LinkID=799012
Open your SQL Server client and connect to your database server by using Windows Authentication or SQL Server authentication
By default, you will get a local DB server called locahost, which will be saved in your local machine. To create a dedicated server, please go through the steps given here : https://docs.microsoft.com/en-us/azure/sql-database/sql-database-get-started
To understand how to create a login, please visit https://msdn.microsoft.com/en-in/library/aa337562.aspx
Once your database is created, it should be visible in object explorer, within Databases. Right click on your database name, and open New Query Window. Then create one or more tables, which you want to use for storing and retrieving your game data. Below is a simple query that lets you create a table called Products. ( Source - https://msdn.microsoft.com/en-us/library/ms365315.aspx )
CREATE TABLE dbo.Products
ProductID int PRIMARY KEY NOT NULL,
ProductName varchar(25) NOT NULL,
Price money NULL,
ProductDescription text NULL
Before we begin writing our SQL queries and executing them in our database, we first need to set up a connection within UE4. This is a onetime setup, and is similar to how we manually connect to our DB Server using Windows or SQL Server Authentication, as shown in the above steps.
To set up a DB Connection, we either need to use Windows Authentication mode or SQL Authentication mode.
Windows Authentication mode authenticates users to use the Database server via Windows Login ID and Password.
|Get Current User||This function returns the current Windows User Name.|
|Get Current Domain||This function returns the current PC Domain.|
|Authenticate User||This function takes the current user name, domain and password for your Windows account as input and returns the authentication result as a Boolean variable. Please note that this function will not work if the current Windows user does not require any password to login. So you may want to avoid using this function unless absolutely necessary.|
SQL Server Authentication mode uses logins that are created and stored in SQL Server and are not based on Windows user accounts. Here you need to explicitly enter both your SQL Server ID and password, while setting up the connection property. To learn about Authentication modes in detail, you can refer here : https://msdn.microsoft.com/en-us/library/ms144284.aspx
Connection to the SQL Server happens asynchronously. To connect to your server, first you need to call the Set Connection Properties function, and then add a custom tick event that you need to listen to at every tick to validate the current status of your connection
The function Set Connection Properties takes the necessary parameters that your server is going to need , and request for the connection to be established.
|Server||This parameter takes your server address.|
|DBName||This parameter takes the name of your database.|
|UserID||This parameter takes your login ID.|
|Password||This parameter takes your login Password.|
|Extra Param||This parameter takes any extra setting that you might want to add. All individual settings should be seperated by semicolon and passed as a single string.|
|Is Trusted||If you are using Windows Authentication Mode , you can check Is Trusted, and you do not need to enter your User ID and password in that case. Otherwise, uncheck this paramter.|
A connection string typically looks like:
So if your connectionstring consists of extra parameters , apart from the usual parameters included in the Set Connection Properties function(Server , DBName , UserID , Password , IsTrusted) , you can enter those extra parameters in the Extra Param input, seperated by a semicolon. For example , if your connectionstring is something like :
Then you need to enter , in your Extra Param input :
It must be noted here that you can obviously enter the extra parameters in other inputs as well, like in your Server input , you can also write like this :
But it is recommended to use Extra Param input for this purpose, just to avoid confusion.
The Server property is not limited to any particular SQL Server type and can accept different string formats, as shown below:
|Server Type||String Format|
|Standard Security , TCP/IP||
|Named Pipes||np:hostname , np:\\hostname\pipe\pipeName|
To learn about various types of Connection String and forming the one that best suits your requirement, you can refer here : https://www.connectionstrings.com/sql-server/
Once your connection has been established, you need to periodically listen to the function Check Connection State to find out the current state of your connection. An useful way to do that in Blueprints is using Gates, which is called in Tick function , but is opened after the funciton Set Connection Properties is called.
|Broken||The connection to the data source is broken.|
|Closed||The connection is closed.|
|Connecting||The connection object is connecting to the data source.|
|Executing||The connection object is executing a command.|
|Fetching||The connection object is retrieving data.|
|Open||The connection is open.|
As long as the current state shows Connecting, you can execute other operations, and let the connection happen in the background. When you get current state as Open, this means connection is successfully established , and now you can proceed with executing queries in your database.
Once the connection property has been set, you can now start forming your SQL queries. Forming queries can be categorized in two types, queries that modify data to the database (INSERT, UPDATE, DELETE), or the queries that fetch data from the database (SELECT). We will discuss these two categories one by one.
Let us say we have created a table in our database called Products by using the below query:
CREATE TABLE dbo.Persons()
ID int NOT NULL IDENTITY(1,1),
LastName varchar(255) NOT NULL, NickName nvarchar(50) ,
Age int, DateofBirth datetime ,
PRIMARY KEY (ID));
We will take this table as a reference to form our queries and discuss the two categories of query formation.
To asynchronously modify data in our table , we will use the function Update data from Query Async.
After calling the Update function , you need to periodically listen to the function Check Query Execution State to find out the current state of your function. Here also you can use Gate within the Tick function, which should be opened after the function Update data from Query Async is called.
|Query Execution State||Description|
|Executing||The query is still being executed.|
|Success||The query has been successfully executed.|
|Failed||The query execution has failed. This generates error message as string output in the Check Query Execution State function.|
If you would like to update data synchronously, you can call the function Update data from Query which takes a string input where we have to enter our query and executes the query in the database.