UserId,密码或帐户是无效的Teradata .Net Connection

时间:2017-12-14 14:11:25

标签: c# asp.net teradata

以下代码会抛出错误

  

UserId,密码或帐户无效。

代码行adapter.Fill(ds);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Teradata.Client.Provider;
using System.Data;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        TdConnectionStringBuilder connectionStringBuilder = new TdConnectionStringBuilder();
        connectionStringBuilder.DataSource = "URL";
        connectionStringBuilder.Database = "DB";
        connectionStringBuilder.UserId = "USERNAME";
        connectionStringBuilder.Password = "PASSWORD";
        connectionStringBuilder.AuthenticationMechanism = "LDAP";


        TdConnection cn = new TdConnection();

        cn.ConnectionString = connectionStringBuilder.ConnectionString;
        cn.Open();

        TdCommand cmd = new TdCommand("EXEC MACRO", cn);
        TdDataReader reader = cmd.ExecuteReader();
        TdDataAdapter adapter = new TdDataAdapter(cmd.CommandText, cn.ConnectionString);
        DataSet ds = new DataSet();
        adapter.Fill(ds);

       // myLabel.Text= ds.Tables[0].Rows[0]["event_id"].ToString();

        cmd.Dispose();
        cn.Close();

    }
}

但是,下面的代码完全正常,并按预期返回值。

TdConnectionStringBuilder connectionStringBuilder = new TdConnectionStringBuilder();
            connectionStringBuilder.DataSource = "URL";
            connectionStringBuilder.Database = "DB";
            connectionStringBuilder.UserId = "USERNAME";
            connectionStringBuilder.Password = "PASSWORD";
            connectionStringBuilder.AuthenticationMechanism = "LDAP";

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

        TdCommand cmd = new TdCommand("Show table DB.TABLE1", cn);
        String customers = (String)cmd.ExecuteScalar();
        MeanTime.Text = customers;
        cmd.Dispose();
        cn.Close();

用户ID,密码,数据源等都是相同的,但在第一个代码上失败但在第二个代码上正常运行。

1 个答案:

答案 0 :(得分:0)

调用proc和宏时,可以使用TDCommand.ExecuteNonQuery

更新:在进一步阅读here时,您似乎可以在使用存储过程命令类型时包含CALL。宏执行的正确Commandtype是System.Data.CommandType.Text,您可能需要EXEC

作为参考,通过参数绑定和程序,下面是一个工作示例。对于宏执行,需要进行一些小的调整(如上所述)。

//create a new td connection
TdConnection cn = new TdConnection();

//Build the connection string
TdConnectionStringBuilder connectionStringBuilder = new TdConnectionStringBuilder();
connectionStringBuilder.DataSource = "serveraddress";
connectionStringBuilder.Database = "defaultdatabase";
connectionStringBuilder.UserId = "username";
connectionStringBuilder.Password = "password";
connectionStringBuilder.AuthenticationMechanism = "LDAP";
connectionStringBuilder.CommandTimeout = 120; 

//Open the connection
cn.ConnectionString = connectionStringBuilder.ConnectionString;
cn.Open();

// Initialize TdCommand from the tdconnection
TdCommand cmd = cn.CreateCommand();

//CommandText is set to Stored Procedure name, in this case,
//.NET Data Provider will generate the CALL statement.
cmd.CommandText = "yourdatabasename.yourprocname";
cmd.CommandType = CommandType.StoredProcedure;

// Create Input Parameter required by this procedure
TdParameter InParm1 = cmd.CreateParameter();
InParm1.Direction = ParameterDirection.Input;
InParm1.DbType = DbType.String;
InParm1.Size = 20;
InParm1.Value = "yourparamvalue";

//and bind it
cmd.Parameters.Add(InParm1);

//------------OPTION 1 CATCHING AN OUTPUT PARAMETER---------------------
    //If you are catching an output parameter from a proc then create here:
    // Create Output Parameter.
    TdParameter OutParm = cmd.CreateParameter();
    OutParm.Direction = ParameterDirection.Output;
    OutParm.ParameterName = "myOutputParam";
    OutParm.DbType = DbType.String;
    OutParm.Size = 200;
    cmd.Parameters.Add(OutParm);

    // Run it up
    cmd.ExecuteReader()

    //if this is returning a single value you can grab it now:
    myOutput = OutParm.Value.ToString();


//------------OPTION 2 CATCHING A RECORDSET-----------------------------

    //list based on class set in seperate model
    List<myClass> l_myclass = new List<myClass>();

    //run it up and catch into a TDDataRead
    using (TdDataReader r = cmd.ExecuteReader())
    {
        if (r.HasRows)
        {
            //Loop the result set and catch the values in the list
            while (r.Read())
            {
                //"myclass" class defined in a seperate model
                //Obviously you could do whatever you want here, but
                //creating a list on a class where the column1-column4 is defined makes short work of this. 
                //Then you can dump the whole l_myclass as json back to the client if you want.
                myClass i = new myClass();
                i.column1 = (!r.IsDBNull(0)) ? r.GetString(0) : string.Empty;
                i.column2 = (!r.IsDBNull(1)) ? r.GetString(1) : string.Empty;
                i.column3 = (!r.IsDBNull(2)) ? r.GetString(2) : string.Empty;
                i.column4 = (!r.IsDBNull(3)) ? r.GetString(3) : string.Empty;
                l_myClass.Add(i);
            }
        }
    }

    //Dump the list out as json (for example)
    return Json(l_myClass, System.Web.Mvc.JsonRequestBehavior.AllowGet); 

我个人仅在程序中使用了此功能,但the documentation我已经阅读过,这表明这也是宏的正确途径。