Making a DB Connection Using Service Test 11’s Custom Code
I’ve been getting this question a lot lately: “How can I connect to a database from Service Test 11?” Actually, it would be nice if Service Test had a database activity. It doesn’t, unfortunately, but the good news is that it’s fairly easy to create one.
There are a few ways to do this in C#, but for this example, I’ll be using ODBC.
DSN Setup:
First, make sure you have an ODBC DSN setup under Windows Admin ToolsODBC. I’m using mySQL for this example:
Database Info:
I also have mySQL running on my local machine with a database named ‘servicetest’ that has a table named ‘customers’:
Custom Code:
Drag a custom code activity onto your main canvas area in Service Test, then go to its events and select ‘create a default handler’ from the ExecuteEvent. In the ‘SharedUserCode’ area, we need to first import the ODBC and Forms libraries:
using System.Windows.Forms;
using System.Data.Odbc;
Next, type the following code to create a connection to the DB and return a record:
//Create a string variable that holds your database connection info.
string strConnect = “DSN=yourDSNName;UID=yourDBUserName;PWD=yourDbPassword;DATABASE=yourDbName”;
//Create a connection object
OdbcConnection dbMySQL = new OdbcConnection(strConnect);
//Open a connection
dbMySQL.Open();
//Create a OdbcCommand that will hold the sql statement to execute
OdbcCommand sqlCommand = dbMySQL.CreateCommand();
sqlCommand.CommandText = “select * from customers”;
//Create a sqlReader object which provides a way to read the data rows returned from the data source
OdbcDataReader sqlReader = sqlCommand.ExecuteReader();
//Loop through all the returned recordsets
while (sqlReader.Read())
{
//Show the second column value in the current row. In this example it would return the Name field.
MessageBox.Show(sqlReader.GetString(1));
}
The final code in Service Test should look like this:
To learn how to pass data between activities in Service Test 11, check out my blog post on how to use the custom code functionality.
For my fellow bibliomaniacs who may be new to C#, I would also recommend these two books:
- Beginning C# 3.0: An Introduction to Object Oriented Programming (Wrox Beginning Guides)
- A Tester’s Guide to .NET Programming (Expert’s Voice)
Joe Colantonio is the founder of TestGuild, an industry-leading platform for automation testing and software testing tools. With over 25 years of hands-on experience, he has worked with top enterprise companies, helped develop early test automation tools and frameworks, and runs the largest online automation testing conference, Automation Guild.
Joe is also the author of Automation Awesomeness: 260 Actionable Affirmations To Improve Your QA & Automation Testing Skills and the host of the TestGuild podcast, which he has released weekly since 2014, making it the longest-running podcast dedicated to automation testing. Over the years, he has interviewed top thought leaders in DevOps, AI-driven test automation, and software quality, shaping the conversation in the industry.
With a reach of over 400,000 across his YouTube channel, LinkedIn, email list, and other social channels, Joe’s insights impact thousands of testers and engineers worldwide.
He has worked with some of the top companies in software testing and automation, including Tricentis, Keysight, Applitools, and BrowserStack, as sponsors and partners, helping them connect with the right audience in the automation testing space.
Follow him on LinkedIn or check out more at TestGuild.com.
Related Posts
Look, I’ve been doing test automation for over 25 years. I’ve heard the predictions. “Manual testing is dead.” “AI will […]
The 72.8% Paradox That Changes Everything After interviewing 50+ testing experts in 2025 and analyzing data from our 40,000+ member […]
Look, I’ve been doing this testing thing for over 25 years now. I first wrote about the AI “three waves” […]
What’s New in Low-Code/No-Code Testing for 2026 A lot had changed on the The low-code/no-code testing space and it has […]




