Having issues connecting DB2 with .NET

1153005 pts.
IBM iSeries
I'm trying to connect to a DB2 database (an iSeries version) using .NET by referencing a .DLL and not installing software on my server. I currently have IBM.Data.DB2.iSeries.dll installed as part of iSeries access for Windows.

Is there an easy way to connect from .NET to DB2 by referencing a .DLL in my code? I know it's possible using Java but what about .NET? I can give you an example of how we currently use IBM.Data.DB2.iSeries.dll.
System.Data.IDbConnection connection = null;
IDataReader reader = null;
connection = new iDB2Connection(ConfigurationManager.ConnectionStrings.ConnectionString);
IDbCommand command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = sql;
reader = command.ExecuteReader();
try { reader.Close(); } catch (Exception ex) { }
try { connection.Close(); }catch (Exception ex) { }
Thanks for all the help!

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 2  Replies

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • ToddN2000

    I wrote a little .NET that uses a DB2 connection...Remember you need to add the .net reference "IBM DB2 for i.NET Provider"

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="VB_test_code._Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>SQL results from DB2</title>
        <form id="form1" runat="server">
    <asp:TextBox ID="tbxSQL" runat="server" Columns="80" Rows="9" TextMode="MultiLine"></asp:TextBox>
    <br />
    <asp:Label ID="lblError" runat="server"></asp:Label>
    <br />
    <asp:Button ID="btnRunSQL" runat="server" Text="Run SQL Script" />
    <br />
    <asp:GridView ID="dgResults" AutoGenerateColumns="true" OnRowDataBound="dgResults_RowDataBound" runat="server">
    Imports System
    Imports System.Data
    Imports System.IO
    Imports IBM.Data.DB2.iSeries
    Partial Public Class _Default
        Inherits System.Web.UI.Page
        Public mySQL As String = " "
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                lblError.Visible = False
            End If
        End Sub
        Private Sub btnRunSQL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRunSQL.Click
            Dim mySQL As String = Request.Form("tbxSQL")
            Dim strConnection As String = "DataSource=;userid=LIVE;password=LIVE1;DefaultCollection=MYDATALIB;"
            Dim myConnection As New iDB2Connection(strConnection)
            Dim myCommand As New iDB2Command(mySQL, myConnection)
            Dim myReader As iDB2DataReader
                myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
                If myReader.HasRows = False Then
                    lblError.Text = "No rows returned for SQL statement"
                    lblError.Visible = True
                    Dim fields As Integer = myReader.FieldCount
                    dgResults.DataSource = myReader
                    lblError.Text = " "
                    lblError.Visible = False
                    btnRunSQL.Visible = False
                    tbxSQL.Visible = False
                End If
            Catch ex As Exception
                lblError.Text = ex.Message
                lblError.Visible = True
            End Try
        End Sub
        Protected Sub dgResults_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
            For i As Integer = 0 To e.Row.Cells.Count - 1
                e.Row.Cells(i).Attributes.Add("style", "white-space: nowrap;")
        End Sub
    End Class



    135,485 pointsBadges:
  • TomLiotta

    No matter what, the IBM.Data.DB2.iSeries.dll by itself is not enough. It cannot be used by itself because it doesn't provide other necessary services. Security needs to be handled, CCSID conversion need to be done, license checks must be made and various other elements are involved. Technically, you could conceivably create coding to replace the other missing .DLLs, but that's a major licensing violation and hardly worth the amount effort needed.

    Just install the "IBM DB2 for i.NET Provider" and don't spend time otherwise.


    125,585 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: