Introduction

This article demonstrates how to execute SQL queries, stored procedure using ODBC, how to pass a parameter to a stored procedure using ODBC, and return output parameter from stored procedure using ODBC.
ODBC (Open Database Connectivity) driver helps to access data in applications from the database management system.
Prerequisites
Note
In this example, I am using Visual Studio 2019 community edition and the framework version is 4.7.2 with SQL server 2014.
Let’s code.
In this example, we will use two SQL tables and Windows form applications to display the result on the screen.
  1. Customers: - SQL table for customers record
  2. Orders: - SQL table for orders of customers
  3. .NET Framework Windows Form application screen like below.

    Executing SQL Queries And SQL Stored Procedure With ODBC
Show a button for showing the record on the screen.

Execute the SELECT command using ODBC

In the below example, I have added an inline ODBC connection string to make the connection between database and application. We can also use ODBC DSN for making a connection between the database and the application.
Execute the following code to check the result for SELECT command.
Executing SQL Queries And SQL Stored Procedure With ODBC
  1. private void BtnShow_Click(object sender, EventArgs e)
  2. {
  3. #region Execute SELECT command using ODBC: -
  4. OdbcConnection conn = new OdbcConnection();
  5. DataSet ds = new DataSet();
  6. conn.ConnectionString = @"Driver={SQL Server}"
  7. + "Server=server name;DataBase=database name;";
  8. String selectSql = "SELECT * FROM Customers;";
  9. OdbcCommand cmd = new OdbcCommand(selectSql, conn);
  10. OdbcDataAdapter da = new OdbcDataAdapter(cmd);
  11. da.Fill(ds);
  12. dgvODBC.AutoGenerateColumns = true;
  13. dgvODBC.DataSource = ds.Tables[0];
  14. conn.Close();
  15. #endregion
  16. }
Output
Executing SQL Queries And SQL Stored Procedure With ODBC

SQL INNER join using ODBC

Code
Executing SQL Queries And SQL Stored Procedure With ODBC
  1. private void BtnShow_Click(object sender, EventArgs e)
  2. {
  3. #region SQL INNER join using ODBC
  4. OdbcConnection conn = new OdbcConnection();
  5. DataSet ds = new DataSet();
  6. conn.ConnectionString = @"Driver={SQL Server};Server=server name;DataBase=database name;";
  7. String selectSql = "SELECT * FROM Customers C INNER JOIN Orders O ON C.CustomerId= o.CustomerId";
  8. OdbcCommand cmd = new OdbcCommand(selectSql, conn);
  9. OdbcDataAdapter da = new OdbcDataAdapter(cmd);
  10. da.Fill(ds);
  11. dgvODBC.AutoGenerateColumns = true;
  12. dgvODBC.DataSource = ds.Tables[0];
  13. conn.Close();
  14. #endregion
  15. }
Output
Executing SQL Queries And SQL Stored Procedure With ODBC

SQL INSERT query using ODBC

We cannot pass the scaler variable in SQL query with ODBC. If you try to pass that, then it will throw an exception like below.
Executing SQL Queries And SQL Stored Procedure With ODBC
“’ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Must declare the scalar variable "@VariableName’.”
Then, how do we pass a parameter?
Refer to the below code.
Code
Executing SQL Queries And SQL Stored Procedure With ODBC
  1. private void BtnShow_Click(object sender, EventArgs e)
  2. {
  3. OdbcConnection conn = new OdbcConnection();
  4. DataSet ds = new DataSet();
  5. conn.ConnectionString = @"Driver={SQL Server};Server=DESKTOP-H9JA9S4\SQLEXPRESS;DataBase=EmployeeDB;";
  6. String insertSql = "INSERT INTO Customers(CustomerId, Name, City, OrderId) VALUES(?, ?, ?, ?)";
  7. OdbcCommand cmd = new OdbcCommand(insertSql, conn);
  8. conn.Open();
  9. cmd.Parameters.Add("CustomerId", OdbcType.Int).Value = 3;
  10. cmd.Parameters.Add("Name", OdbcType.VarChar).Value = "Admin";
  11. cmd.Parameters.Add("City", OdbcType.VarChar).Value = "Hyderabad";
  12. cmd.Parameters.Add("OrderId", OdbcType.Int).Value = 4;
  13. cmd.ExecuteNonQuery();
  14. String selectSql = "SELECT * FROM Customers;";
  15. cmd = new OdbcCommand(selectSql, conn);
  16. OdbcDataAdapter da = new OdbcDataAdapter(cmd);
  17. da.Fill(ds);
  18. dgvODBC.AutoGenerateColumns = true;
  19. dgvODBC.DataSource = ds.Tables[0];
  20. conn.Close();
  21. }
Output
Executing SQL Queries And SQL Stored Procedure With ODBC
For Insert and Update queries also, we can use the same technique.

SQL Stored Procedure using ODBC

In this example, we will add a stored procedure with name GetCustomerDetails which accepts a parameter as id and based on Id, we will get a customer's record.
SQL query for the stored procedure.
Executing SQL Queries And SQL Stored Procedure With ODBC
  1. CREATE PROCEDURE [dbo].[GetCustomerDetails]
  2. (
  3. @ID INT
  4. )
  5. AS BEGIN
  6. SELECT * FROM Customers C INNER JOIN Orders O ON C.CustomerId= o.CustomerId
  7. WHERE C.CustomerId = @ID
  8. END
  9. GO
C# Code
Executing SQL Queries And SQL Stored Procedure With ODBC
  1. private void BtnShow_Click(object sender, EventArgs e)
  2. {
  3. OdbcConnection conn = new OdbcConnection();
  4. OdbcCommand cmd = new OdbcCommand();
  5. DataSet ds = new DataSet();
  6. conn.ConnectionString = @"Driver={SQL Server};Server=servername;DataBase=database name;";
  7. cmd.Connection = conn;
  8. cmd.CommandType = System.Data.CommandType.Text;
  9. cmd.CommandText = "EXEC dbo.GetCustomerDetails @ID=? ";
  10. cmd.Parameters.Add("?", OdbcType.Int).Value = 2;
  11. OdbcDataAdapter da = new OdbcDataAdapter(cmd);
  12. da.Fill(ds);
  13. dgvODBC.AutoGenerateColumns = true;
  14. dgvODBC.DataSource = ds.Tables[0];
  15. conn.Close();
  16. }
Output
Executing SQL Queries And SQL Stored Procedure With ODBC

SQL Stored Procedure with OUTPUT parameter using ODBC

In this example, we will alter the stored procedure and add one more parameter to return the name of the customer based on customer id.
SQL query for the stored procedure.
Executing SQL Queries And SQL Stored Procedure With ODBC
  1. CREATE PROCEDURE [dbo].[GetCustomerDetails]
  2. (
  3. @ID INT,
  4. @Name VARCHAR(20) OUTPUT
  5. )
  6. AS BEGIN
  7. SELECT C.Name FROM Customers C INNER JOIN Orders O ON C.CustomerId= o.CustomerId
  8. WHERE C.CustomerId = @ID
  9. END
  10. GO
C# Code
Executing SQL Queries And SQL Stored Procedure With ODBC
  1. private void BtnShow_Click(object sender, EventArgs e)
  2. {
  3. OdbcConnection conn = new OdbcConnection();
  4. OdbcCommand cmd = new OdbcCommand();
  5. DataSet ds = new DataSet();
  6. conn.ConnectionString = @"Driver={SQL Server};Server=server name;DataBase=database name;";
  7. cmd.Connection = conn;
  8. cmd.CommandType = System.Data.CommandType.Text;
  9. cmd.CommandText = "DECLARE @CustomerName VARCHAR(20) "
  10. + "EXEC dbo.GetCustomerDetails @ID=?, @Name=@CustomerName OUTPUT";
  11. cmd.Parameters.Add("Id", OdbcType.Int).Value = 2;
  12. OdbcDataAdapter da = new OdbcDataAdapter(cmd);
  13. da.Fill(ds);
  14. dgvODBC.AutoGenerateColumns = true;
  15. dgvODBC.DataSource = ds.Tables[0];
  16. conn.Close();
  17. }
Output
Executing SQL Queries And SQL Stored Procedure With ODBC

Summary

In this article, I demonstrated how to execute SQL query and stored procedure using ODBC, pass a parameter to a stored procedure using ODBC, and return output parameter from stored procedure using ODBC. Hopefully, it will help.