Having issues connecting DB2 with .NET

344855 pts.
Tags:
.NET
DB2
DLL
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.
String sql = "SELECT 1 FROM SCHEMAX.TABLEX";
System.Data.IDbConnection connection = null;
IDataReader reader = null;
try
{
connection = new iDB2Connection(ConfigurationManager.ConnectionStrings.ConnectionString);
connection.Open();
IDbCommand command = connection.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = sql;
reader = command.ExecuteReader();
}
finally
{
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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • 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>
    </head>
    <body>
    
        <form id="form1" runat="server">
        
    <div>
    <asp:TextBox ID="tbxSQL" runat="server" Columns="80" Rows="9" TextMode="MultiLine"></asp:TextBox>
    <br />
    </div>
    
    <div>
    <asp:Label ID="lblError" runat="server"></asp:Label>
    <br />
    </div>
    
    <div>
    <asp:Button ID="btnRunSQL" runat="server" Text="Run SQL Script" />
    <br />
    </div>
         
    <div>
    <asp:GridView ID="dgResults" AutoGenerateColumns="true" OnRowDataBound="dgResults_RowDataBound" runat="server">
    </asp:GridView>
    </div>
        
        </form>
        
    </body>
    </html>
    
    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=10.10.10.10;userid=LIVE;password=LIVE1;DefaultCollection=MYDATALIB;"
    
            Dim myConnection As New iDB2Connection(strConnection)
            Dim myCommand As New iDB2Command(mySQL, myConnection)
            myCommand.Connection.Open()
    
            Dim myReader As iDB2DataReader
    
            Try
                myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    
                If myReader.HasRows = False Then
                    lblError.Text = "No rows returned for SQL statement"
                    lblError.Visible = True
                Else
    
                    Dim fields As Integer = myReader.FieldCount
    
                    dgResults.DataSource = myReader
                    dgResults.DataBind()
    
                    myReader.Close()
                    myConnection.Close()
    
                    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;")
            Next
        End Sub
    
    End Class

     

     

    9,700 pointsBadges:
    report
  • 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.

    Tom

    125,585 pointsBadges:
    report

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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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

Following