user1420914
user1420914

Reputation: 279

Connect to as400 using sql

I would think this would be a fairly common thing, and easy to find in google, but so far I haven't had much luck. I would like my application to connect to the i-series AS400 system using some method, run an SQL statement over an AS400 physical file, return a result set, and then have my visual c# program process the result set. I've heard of ADODB, ODBC, DB2, and OLEDB. Can someone show me an example of the syntax for getting one of these methods to work? I'd prefer to use a method that isn't dependent on having certain software such as client access, and am trying to avoid using something like ODBC due to the fact you have to configure the DSN. I've searched and searched, but the most code I can find is what the connection string should look like. Any help is appreciated!

Thanks!

Upvotes: 1

Views: 2631

Answers (1)

Muhammad Hani
Muhammad Hani

Reputation: 8664

I've found this question during my search.

You can connect to the IBM iSeries using OLEDB connection and run SQL queries then retrieve the results but you need some steps first.

  1. You need the AS400 .Net data provider. - http://www-03.ibm.com/systems/power/software/i/access/windows/dotnet.html

  2. You need to write your connection string. - http://www.connectionstrings.com/as-400

  3. Some Code, then

    string ConnectionString = AS400ConnectionString;
    OleDbConnection _Connection = new OleDbConnection(ConnectionString);
    OleDbCommand _Command = _Connection.CreateCommand();
    
                string strQuery = string.Empty;
                strQuery += @"SELECT * FROM Contacts";
    
                if (string.IsNullOrEmpty(strQuery))
                {
                    throw (new Exception("No Library Setup"));
                }
    
                _Command.CommandText = strQuery;
                if (_Connection.State != ConnectionState.Open)
                    _Connection.Open();
    
                OleDbDataReader reader = _Command.ExecuteReader();
    
                while (reader.Read())
                {
                    //Your Logic
                }
    
                reader.Close();
                if (_Connection.State != ConnectionState.Closed)
                    _Connection.Close();
    

Upvotes: 2

Related Questions