Skip to content

Unable to connect to AS400 in C#

Brian

Asked Aug 3, 2007, 11:27 AM EDT1 answer
Share
LinkedInX
I am trying to connect to an AS400 database from C# code and cannot find what I am doing wrong.
This is the error that I get (thrown by the conn.Open()):
"Non-NULL controlling IUnknown was specified, and either the requested interface was not IUnknown, or the provider does not support COM aggregation."

Here is the body of my code:
String connectionString = "Provider=IBMDA400;Data Source=myAS400;User ID=myUser;Password=myPassword;";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString);
conn.Open();   <--- this is where it throws an Exception.
conn.Close();

Here is what I don't understand -- I am able to create a connection to the AS400 through the 'Server Explorer' with these settings:
Data Source: .NET Framework Data Provider for OLE DB
OLE DB Provider: IBM AS400 OLE DB Provider
Server: myAS400
User: myUser
Pass: myPassword

Any ideas what my problem is - and how I might be able to create that same connection within my C# code.

Thanks!

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Here is the solution found:
    From Windows:
    Go into Admin Tools > Data Sources (ODBC)
    Select the System DSN Tab and Add a new System Data Source
    Select the ODBC driver used to connect to the AS400
    Give it a Data Source Name, AS400 System Name, Library List (*USRLIBL)

    From a c# program:
    using System.Data.Odbc;

    String connectionString = "Dsn=myAS400dsn;uid=myUser;pwd=myPassword";
    OdbcConnection conn = new OdbcConnection(connectionString);
    conn.Open();

    string queryString = "SELECT * FROM myLibrary.myTable";
    OdbcCommand command = new OdbcCommand(queryString, conn);
    OdbcDataReader rs = command.ExecuteReader();

    while (rs.Read())
    {
      Console.WriteLine(rs.GetString(0));
    }

    conn.Close();
    0