There are multiple ways to access Db2 from .NET:
System.Data.Odbc
to access Db2 natively with Mono running on IBM i. The advantage of this driver is that the code can also run on Windows and Linux; just change the connection string. You can download it from IBM without a support contract.
Build this with mcs /r:System.Data.dll odbctest.cs
. Run with mono odbctest.exe
.
Note that without the latest PTFs to IBM i, you must authenticate by including a username and password in the connection string. If you're running IBM i 7.4 or the latest PTFs (SI68113 for 7.3, SI69058 for 7.2), you can omit that section of the code, and just use a connection string of DSN=*LOCAL
.
// odbctest.cs using System; using System.Data; using System.Data.Odbc; public class Test { public static void Main(string[] args) { // For compatbility with older versions of IBM i // without the PTF, we have to authenticate as if we // were on a remote system. For a remote system, you // need to specify a DSN pointed to your host or the // Client Access ODBC driver and the hostname. string password = "", username = ""; Console.Write("Username: "); username = Console.ReadLine(); Console.Write("Password: "); password = Console.ReadLine(); // The DSN is installed by the PASE driver to refer to // the Client Access driver pointed locally. Append the // necessary authentication in the connection string. string connectionString = "DSN=*LOCAL;" + "UID=" + username + ";PWD=" + password; IDbConnection dbcon; dbcon = new OdbcConnection(connectionString); dbcon.Open(); Console.WriteLine (" ** Connected"); Console.WriteLine (" ** Version {0}", ((OdbcConnection)dbcon).ServerVersion); IDbCommand dbcmd = dbcon.CreateCommand(); // This example table exists by default on IBM i. string sql = "SELECT LSTNAM, INIT FROM QIWS.QCUSTCDT"; dbcmd.CommandText = sql; IDataReader reader = dbcmd.ExecuteReader(); while(reader.Read()) { string lastName = (string) reader["LSTNAM"]; string initials = (string) reader["INIT"]; Console.WriteLine("Name: " + lastName + " " + initials); } // clean up reader.Close(); reader = null; dbcmd.Dispose(); dbcmd = null; dbcon.Close(); dbcon = null; } }
This should be at the same level of support. Due to Kerberos support not being built yet, the stock Mono System.Data.SqlClient
driver might not work; but you may have luck with alternative drivers.
SQLite should work as well.
TODO.