access_time 5 years ago visibility1367 comment 0 languageEnglish
more_vert

In my previous post, I demonstrated how to use ADO.NET to connect to Teradata in your OLTP projects.  In this sample, I am going to implement the same sample using ADO.NET Entity Model, Entity Framework and LINQ.

Connect to Teradata Database via .NET Data Provider

Prerequisites

Step 1 Create ADO.NET Entity Data Model

In the wizard, create ‘TDSample.edmx’ through item ‘ADO.NET Entity Data Model’.

image

Select Generate from Database:

image

Create a connection to Teradata database:

image

Remember to set the option ‘Use X Views’ to false and set the Database to the database you are using. I also changed the timeout options as my local VM responds slowly.

The entity connection string looks like:

metadata=res://*/TDSample.csdl|res://*/TDSample.ssdl|res://*/TDSample.msl;provider=Teradata.Client.Provider;provider connection string="connection timeout=90;database=TD_MS_SAMPLE_DB;user id=dbc;data source=192.168.121.128;persist security info=True;use x views=False;restrict to default database=True;command timeout=90"

* Change the server and user id/password to yours.

Click next to choose the entity framework version.

image

And then choose the right tables required.

image

Click Finish button to close the wizard. In my sample, the model has two tables added.

image

Step 2 LINQ to Teradata

With the generated entity objects, it will be very straightforward to query from the database.

The sample code looks like:

static void Main(string[] args)
        {
            DisplayEmployeeInfoFromTeraData2();
            Console.ReadLine();

        }

        private static void DisplayEmployeeInfoFromTeraData2()
        {
            var db = new TDSampleContainer();
            var query = from emp in db.Employees.AsQueryable()
                        select new
                        {
                            EmployeeID = emp.EmployeeID,
                            EmployeeName = emp.EmployeeName,
                            EmployeeSalutation = emp.Gendar == "M" ? "Dear MR " + emp.EmployeeName :
                                      "Dear MS " + emp.EmployeeName
                        };
            foreach (var e in query)
            {
                Console.WriteLine("EmployeeID = {0}", e.EmployeeID);
                Console.WriteLine("EmployeeName = {0}", e.EmployeeName);
                Console.WriteLine("Employee Salutation = {0}", e.EmployeeSalutation);
                Console.WriteLine();
            }

            Console.WriteLine("{0} Records", query.Count());
        }

The result

image

Summary

With .NET Data Provider for Teradata, you can easily operate on the database with Linq. If you want to learn more about LINQ, please visit the following link:

https://msdn.microsoft.com/en-us/library/bb397926.aspx

info Last modified by Raymond at 5 months ago * This page is subject to Site terms.

More from Kontext

local_offer dotnet core

visibility 578
thumb_up 0
access_time 2 years ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

open_in_new C# Programming

local_offer C#

visibility 438
thumb_up 0
access_time 3 years ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

open_in_new C# Programming

local_offer hadoop local_offer hdfs

visibility 1789
thumb_up 0
access_time 3 years ago

Background Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem / ...

open_in_new C# Programming

local_offer C#

visibility 361
thumb_up 0
access_time 3 years ago

C# is my favourite programming language and has been evolving overtime. I have been programming with many other languages but C# is always my favourite because it is simple and easy to use. Visual Studio is the most powerful tool I’ve ever used. To me, programming efficiency and peformance are th...

open_in_new C# Programming

info About author

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward