简体   繁体   English

如何在c#中设置.net teradata连接?

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

I am trying to connect to Teradata with c#. 我想用c#连接到Teradata。 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 (我也尝试过DSN , UID , PWD但是,我得到的例外是我的用户ID,帐号或密码不正确 ...但我能够轻松地使用SQL Assistant登录。所以,我排除了不正确的用户ID或密码

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? 任何人都可以给我一个工作示例代码吗?

Based on the link you posted, changing the Authentication Mechanism to LDAP might work. 根据您发布的链接,将身份验证机制更改为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);
    }
}

Try this. 试试这个。 try to see if you get anything in the string 'tt'. 试着看看你是否在字符串'tt'中得到任何东西。 Plz change your DBCommand query to whatever relevant. Plz将您的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();

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得c#到teradata的正确连接字符串? - How to get the correct connection string for c# to teradata? 如何基于 C# 中的标记 VLAN 建立 TCP 连接? - How to set up a TCP connection based on a tagged VLAN in C#? 在Teradata .net C#中执行宏 - Executing Macro in Teradata .net C# 如何在C#和Java之间正确设置ssl socket连接? - How to properly set up ssl socket connection between C# and Java? 如何使用MS SQL 2008和MS VStudio 2010 C#在LAN连接中设置数据库? - How to set up a database in a LAN connection using MS SQL 2008 and MS VStudio 2010 C#? 如何在不使用对话框的情况下为C#项目中的MySQL数据库设置连接字符串? - How do I set up a connection string for a MySQL database in a C# project without using dialog boxes? WM和C#:如何以编程方式设置WiFi Ad-Hoc连接? - WM and C#: How to set up a WiFi Ad-Hoc connection programmatically? 如何在c#.net中集中数据库连接? - How to central the db connection in c# .net? 如何在 C# 中设置 ADODB 连接属性 - How to set ADODB connection property in C# 如何从C#代码后面为akka.net持久性参与者设置连接字符串 - how to set connection string for akka.net persistence actor from C# code behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM