10

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Aleksei Nikolaevich
  • 325
  • 3
  • 15
  • 40
  • Your userid and password might be correct. Are you sure you are using the correct DSN ? – Buras Jan 17 '14 at 19:56
  • I am sure. I have already established successfull connection using Java. And the DSN that I used was OK – Aleksei Nikolaevich Jan 17 '14 at 19:57
  • Sometimes teradata has issues if you have special characters in your password. Do you have any ? – Buras Jan 17 '14 at 19:58
  • I know about this. Actually that was an issue when I was trying to establish connection using Java. My password is very simple. Actually it is : `testPassCd33` So there is no issue there – Aleksei Nikolaevich Jan 17 '14 at 19:59
  • Does your DSN start with `www. ` – Buras Jan 17 '14 at 19:59
  • No. I have odbc. So there is no need. I already did it in Java. The DSN is correct. – Aleksei Nikolaevich Jan 17 '14 at 20:00
  • 1
    Error 8017 "The UserId, Password or Account is invalid." indicates that the server was found but the logon failed. You should check dbc.LogonOff for details, logon with the same user and "SELECT * FROM dbc.LogonOffVX WHERE LogDate = DATE ORDER BY LogTime DESC;" – dnoeth Jan 19 '14 at 11:15

4 Answers4

10

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);
    }
}
TTat
  • 1,486
  • 8
  • 12
2

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();
Aby
  • 1,916
  • 1
  • 12
  • 18
0

Try this

                TdConnectionStringBuilder builder=new TdConnectionStringBuilder();
                builder.Add("Data Source", "xxx");
                builder.Add("User ID", "vvv");
                builder.Add("Password", "bbb");
                TdConnection cn = new TdConnection(builder.ConnectionString);
                cn.Open();
Donotello
  • 157
  • 2
  • 2
  • 14
0

Copy paste this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teradata.Client.Provider;

namespace Teradata.Client.Provider.ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            TdConnection cn = new TdConnection();
            TdConnectionStringBuilder conStrBuilder = new TdConnectionStringBuilder();

            conStrBuilder.DataSource = "DSN";
            // conStrBuilder.Database = "optional";

            conStrBuilder.UserId = "user"; conStrBuilder.Password = "pass";
            cn.ConnectionString = conStrBuilder.ConnectionString;

            cn.Open();

            Console.WriteLine("connection successfull");
        }
    }
}
Michelangelo
  • 35
  • 1
  • 7