【问题标题】:How to set up .net teradata connection in c#?如何在 C# 中设置 .net teradata 连接?
【发布时间】:2014-02-06 00:38:56
【问题描述】:

我正在尝试使用 c# 连接到 Teradata。我正在使用来自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);
                 }
             }
         }
    }
}

(我也试过DSN , UID , PWD 但是,我收到异常,我的 userid 、帐户或密码不正确 ... 但我可以使用 SQL 助手轻松登录。所以,我排除了错误的用户名或密码

Here I found a possible solution for my problem 但我不知道我的示例代码到底需要更改什么。

所以,我不知道如何实施该解决方案。

谁能给我一个工作示例代码?

【问题讨论】:

  • 您的用户名和密码可能正确。您确定您使用的是正确的 DSN 吗?
  • 我确定。我已经使用 Java 建立了成功的连接。而且我用的DSN还可以
  • 如果您的密码中有特殊字符,有时 teradata 会出现问题。你有吗?
  • 我知道这件事。实际上,当我尝试使用 Java 建立连接时,这是一个问题。我的密码很简单。实际上是:testPassCd33 所以没有问题
  • 错误 8017“用户 ID、密码或帐户无效。”表示找到服务器但登录失败。您应该检查 dbc.LogonOff 以获取详细信息,使用同一用户登录并“SELECT * FROM dbc.LogonOffVX WHERE LogDate = DATE ORDER BY LogTime DESC;”

标签: c# teradata


【解决方案1】:

根据您发布的链接,将身份验证机制更改为 LDAP 可能有效。

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);
    }
}

【讨论】:

    【解决方案2】:

    试试这个。试着看看你是否在字符串 'tt' 中得到任何东西。请将您的 DBCommand 查询更改为相关的任何内容。

        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();
    

    【讨论】:

      【解决方案3】:

      试试这个

                      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();
      

      【讨论】:

        【解决方案4】:

        复制粘贴。

        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");
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2011-07-29
          • 2020-03-15
          • 2020-09-19
          • 2018-09-06
          • 2019-01-07
          • 1970-01-01
          • 1970-01-01
          • 2020-09-24
          • 1970-01-01
          相关资源
          最近更新 更多