Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up .net teradata connection in c#?

Tags:

c#

teradata

I am trying to connect to Teradata with c#. I am using the sample code from this website

using System;
using System.Collections.Generic;
using System.Text;
using Teradata.Client.Provider;

namespace Teradata.Client.Provider.HelloWorld
{
    class HelloWorld
    {
        static void Main(string[] args)
        {
            using (TdConnection cn = new TdConnection("Data Source = x;User ID = y;Password = z;"))
            {
                cn.Open();
                TdCommand cmd = cn.CreateCommand();
                cmd.CommandText = "SELECT DATE";
                using (TdDataReader reader = cmd.ExecuteReader())
                {
                    reader.Read();
                    DateTime date = reader.GetDate(0);
                    Console.WriteLine("Teradata Database DATE is {0}", date);
                 }
             }
         }
    }
}

(I have also tried DSN , UID , PWD However, I am getting exception that either my userid , account or password not correct ... But I am able to login using SQL Assistant easily. So , I rule out incorrect userid or password

Here I found a possible solution for my problem But I do not know what exactly I need to change in my sample code.

So, I have no idea how to implement that solution.

Can anybody give me a working sample code?

like image 420
Aleksei Nikolaevich Avatar asked Jan 16 '14 21:01

Aleksei Nikolaevich


People also ask

What is Teradata connection?

Teradata Database is a relational database management system from Teradata Corporation, typically used to support large data warehousing operations. Connect to on-premise Teradata database to read data from tables.


2 Answers

Based on the link you posted, changing the Authentication Mechanism to LDAP might work.

TdConnectionStringBuilder connectionStringBuilder = new TdConnectionStringBuilder();
connectionStringBuilder.DataSource = "x";
connectionStringBuilder.Database = "DATABASENAME";
connectionStringBuilder.UserId = "y";
connectionStringBuilder.Password = "z";
connectionStringBuilder.AuthenticationMechanism = "LDAP";

using (TdConnection cn = new TdConnection())
{
    cn.ConnectionString = connectionStringBuilder.ConnectionString;
    cn.Open();

    TdCommand cmd = cn.CreateCommand();
    cmd.CommandText = "SELECT DATE";

    using (TdDataReader reader = cmd.ExecuteReader())
    {
        reader.Read();
        DateTime date = reader.GetDate(0);
        Console.WriteLine("Teradata Database DATE is {0}", date);
    }
}
like image 94
TTat Avatar answered Oct 21 '22 15:10

TTat


Try this. try to see if you get anything in the string 'tt'. Plz change your DBCommand query to whatever relevant.

    public readonly String sUser = "UserName";
    public readonly String sPassword = "Password";
    public readonly String sDataSource = "IP Address"; 
    public readonly String sConnection = "Data Source=" + sDataSource + ";User ID=" + sUser + ";Password=" + sPassword;


        DbProviderFactory pf = DbProviderFactories.GetFactory("Teradata.Client.Provider");
        DbConnection con = pf.CreateConnection();
        con.ConnectionString = sConnection ;       
        DbCommand cmd= new DbCommand("select top 10 * from tdb.access_method");
        DbCommand Db = (DbCommand)cmd;
        Db.Connection = con;
        DataSet ds = new DataSet();
        con.Open();
        string tt = (string)Db.ExecuteScalar();
like image 41
Aby Avatar answered Oct 21 '22 14:10

Aby