This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

[C#] Connect to Teradata Database via .NET Data Provider

4336 views 2 comments last modified about 4 years ago Raymond

In this page

In this post, I will demonstrate how to connect to Teradata database via .NET Data Provider for Teradata using C#.

Prerequisites

Install the .NET Data Provider for Teradata from the following link:

Teradata Tools and Utilities Windows Installation Package

Step 1 Create a Console Project

Name it as’' ‘ConsoleApps.Teradata’.

Add reference to library ‘.NET Data Provider for Teradata’ as shown in the following screenshot:

image

The version in my PC is 14.11.0.1. You can always download the latest version from Teradata official site.

Step 2 Create a method to read data from Teradata

Remember to import the namespace ‘Teradata.Client.Provider’.

using System;
using Teradata.Client.Provider;

namespace ConsoleApps.Teradata
{
    class Program
    {
        static void Main(string[] args)
        {
            DisplayEmployeeInfoFromTeraData();
            Console.ReadLine();

        }

        private static void DisplayEmployeeInfoFromTeraData()
        {
        }
    }
}

Step 3 Implement the method

Create connection, command and adapter objects to read data in a standard ADO.NET way.

The sample code will be similar to what I’ve provided here:

private static void DisplayEmployeeInfoFromTeraData()
        {
            var employeeData = new DataSet();

            var connectionString = "Data Source=192.168.121.128;User ID=dbc;Password=dbc;";
            using (var connection = new TdConnection(connectionString))
            {
                var sql = @"SELECT Emp.EmployeeID,
    Emp.EmployeeName,
    
    CASE
        WHEN  Emp.Gendar = 'M' THEN 'Dear MR ' || Emp.EmployeeName ELSE 'Dear MS ' ||Emp.EmployeeName
    END AS ""Employee Salutation""
    FROM TD_MS_SAMPLE_DB.Employee Emp; ";
                var command = new TdCommand(sql, connection);
                var adapter = new TdDataAdapter(command);
                connection.Open();
                adapter.Fill(employeeData);
                connection.Close();

            }

            DisplayEmployeeData(employeeData);
        }

        private static void DisplayEmployeeData(DataSet employeeData)
        {
            if (employeeData.Tables != null && employeeData.Tables.Count > 0)
            {
                var employees = employeeData.Tables[0];
                var columnCount = employees.Columns.Count;
                foreach (DataRow row in employees.Rows)
                {
                    for (int i = 0; i < columnCount; i++)
                    {
                        var col = employees.Columns[i];
                        Console.WriteLine("{0} = {1}", col.ColumnName, row[i]);   
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("{0} Records", employees.Rows.Count);
            }
        }

In this sample, I am connecting to a local Teradata virtual machine with address as ‘192.168.121.128’. One thing to notice is that all the class names are prefixed with ‘Td’, e.g. Connection class is named TdConnection, Command class is named TdCommand and so forth.

Step 4 The result

image

Summary

Teradata is used in many data warehousing projects. If you really wants to use it in your OLTP projects, you can connect to it via .NET Data Provider for Teradata or ODBC Teradata provider. The syntax will be similar as connecting to SQL Server, Oracle or any other databases supported by ADO.NET.

Related pages

Useful DBC (Data Base Computer) System Views in Teradata

182 views   0 comments last modified about 3 months ago

This page summarize some of the commonly used views in Teradata. Conventions In all the views in the following sections, X views are also available though they only return rows that contain information on objects that the requesting database user owns, created, granted privilige on,...

View detail

Install Teradata Express 15.0.0.8 by Using VMware Player 6.0 in Windows

12407 views   23 comments last modified about 5 years ago

In this article, I am going to introduce how to install Teradata Express in virtual machines in Windows. Download software 1) Download VMware Player for Windows 32-bit and 64-bit from the following link (version 6.0): ...

View detail

Connect to Teradata database through Python

1608 views   0 comments last modified about 8 months ago

Teradata published an official Python module which can be used in DevOps projects. More details can be found at the following GitHub site: https://github.com/Teradata/PyTd Install Teradata module ...

View detail

Teradata Tutorials Summary - October 2017

238 views   0 comments last modified about 11 months ago

This post summarizes the tutorials I created in the past about Teradata. It is trying to help you gain the basic SQL skills with Teradata especially if you are a new beginner or you just move from SQL Server or Oracle. It also includes some guides about setting up your own Teradata server (latest Teradata Express version TDExpress16.10.00.03_Sles11_40GB.7z).

View detail

Setup Teradata in Microsoft Azure

215 views   0 comments last modified about 11 months ago

This page provides the steps to create a Teradata database in Microsoft cloud computing platform Azure.

View detail

Teradata SQL Tricks for SQL Server/Oracle Developers

609 views   0 comments last modified about 11 months ago

For many SQL Server or Oracle developers, you may encounter some inconveniences when writing SQL queries. For example, how to select from dummy table or local defined variables. This page summarize the equivalents in Teradata SQL.

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (2)

R Re:[C#] Connect to Teradata Database via .NET Data Provider

Ra*** about 3 years ago

@Neuwa Rakgajane There will be no much difference whether you want to query from one table or from multiple. You can refer to ADO.NET to understand all the operations you can do with Teradata .Net Data Provider. https://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.110).aspx In terms of writing complex SQL query, I was planning to create a serial about querying Teradata but have not completed yet. http://kosmisch.net/Blog/DataAndBusinessIntelligence/Archive/2015/5/3/querying-teradata-and-sql-server-tutorial-1-the-select-statement.html

Neuwa*** about 3 years ago

I have been doing the your tutorial on teradata. Aren't there more tutorial where you query multiple tables at once? I wanted to get a practice on those. Please send me a link if you have such tutorial. I mean more advanced queries, your tutorial were so easy to understand, I need such other complex example urgently.
NR Re:[C#] Connect to Teradata Database via .NET Data Provider

Neuwa*** about 3 years ago

I have been doing the your tutorial on teradata. Aren't there more tutorial where you query multiple tables at once? I wanted to get a practice on those. Please send me a link if you have such tutorial. I mean more advanced queries, your tutorial were so easy to understand, I need such other complex example urgently.