- Created by SS Doc Editor, last modified on Dec 22, 2023
The new SQL Server component comes with code to initialize the connection and execute an SQL query and non-query. The sample code is below:
using System;
using StresStimulus.Extensibility;
using System.Data;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Data.SqlClient;
class MySQLComponent : ActionComponent //Do not rename Class
{
public MySQLComponent() {
//Do not edit this section.
}
/////////////////////////////////////////////////////////////////////////////////////////////
//
// 1. Paste the database connection string between the double quotes of the CONNECTION_STRING constant.
// 2. To call a Select query, use the ExecuteSelectQuery(string query) method with the query as an argument
// 3. To call a Non-query, use the ExecuteNonQuery(string query) method with the query as an argument.
//
/////////////////////////////////////////////////////////////////////////////////////////////
const string CONNECTION_STRING = ""; //TODO: paste the connection string between the double quotes.
#region Start/Stop Test
SqlConnection connection;
/// <summary>
/// Fired when test started.
/// </summary>
public override void OnTestStart() {
connection = new SqlConnection(CONNECTION_STRING);
connection.Open();
}
/// <summary>
/// Fired when test ends.
/// </summary>
public override void OnTestEnd() {
connection.Close();
}
/// <summary>
/// Execute a select SQL Query.
/// </summary>
/// <param name="sql">The SQL statement to query</param>
private void ExecuteSelectQuery(string sql) {
using (var command = new SqlCommand(sql, this.connection)) {
using (var reader = command.ExecuteReader()) {
while (reader.Read()) {
}
}
}
}
/// <summary>
/// Execute a non-query SQL.
/// </summary>
/// <param name="sql">The SQL statement to execute</param>
private void ExecuteNonQuery(string sql) {
using (var command = new SqlCommand(sql, this.connection)) {
command.ExecuteNonQuery();
}
}
#endregion
const string SQL_SELECT_QUERY = ""; //TODO: enter the SQL select query.
const string SQL_NON_QUERY = ""; //TODO: enter the SQL non-select query
/// <summary>
/// A sample action method to execute a select query.
/// </summary>
/// <param name="arg">Action control object that contains the optional argument passed by setting the Argument property or the Action object and return values.</param>
public void SampleSelectQuery(ActionArgs arg) {
ExecuteSelectQuery(SQL_SELECT_QUERY);
}
/// <summary>
/// A sample action method to execute a non-query.
/// </summary>
/// <param name="arg">Action control object that contains the optional argument passed by setting the Argument property or the Action object and return values.</param>
public void SampleExecuteQuery(ActionArgs arg) {
ExecuteNonQuery(SQL_NON_QUERY);
}
}
Add the following to the code:
- The SQL Server connection string in the CONNECTION_STRING constant
- The SQL select query in the SQL_SELECT_QUERY constant
- The SQL non-select query in the SQL_NON_QUERY constant
Overview
Content Tools



