Db2

 View Only
Expand all | Collapse all

Connect from .NET6

  • 1.  Connect from .NET6

    Posted Tue June 21, 2022 09:26 AM
    Edited by System Fri January 20, 2023 04:50 PM
    We have a customer with a Db2 based business system. and we want to develop a .NET 6 application for them, and connect to the Db2 database. I have never worked with IBM Db2 before, and need some help. I have tried installed the IBM NuGet Db2.core, for .NET 6 but it throws errors when I try connect, it throws on the Open command even. So...

    Question: Exactly how do I connect to Db2 from .NET6?


  • 2.  RE: Connect from .NET6

    Posted Thu June 23, 2022 12:12 PM
    Hi, Holmgren

    You can use the Db2 .NET provider for Entity Framework Core to develop .NET applications that use the .NET Core Entity Framework to connect to IBM database servers.
    The IBM Data Server Provider for .NET is a name that is used to describe the .NET providers that are packaged with the IBM data server clients products. There are two .NET providers included in the IBM data server clients or IBM Data Server Driver Package. The two .NET providers are also called the Common .NET Providers, packaged with the IBM data server clients products.
    If you continue encounter errors when connecting to Db2, pls try to ask your customer to open Db2 support case to provide further advice.


    ------------------------------
    Su Li Hou
    ------------------------------



  • 3.  RE: Connect from .NET6

    Posted Mon June 27, 2022 04:33 AM
    Thanks for the well written response!

    I will try out the: "Net.IBM.Data.Db2", seems to be for .NET6. I missed that nuget package. 

    Cheers


  • 4.  RE: Connect from .NET6

    Posted Wed May 24, 2023 10:21 AM

    Have you found a solution to connect using DbProviderFactories?  I cannot figure out the correct .RegisterFactory() parameters.

    Thanks



    ------------------------------
    Eric M
    ------------------------------



  • 5.  RE: Connect from .NET6

    Posted Fri June 23, 2023 07:28 PM

    Hi Su Li Hou,

    i was trying to develop a simple console application to connect to IBM i db2 (V7.4) with Visual Studio (and C sharp) on .NET 6 or 7. i installed NuGet package Net.IBM.Data.Db2 (7.0.0.200). It didn't work even on opening the connection.

    here is the code:

    using System.Data.Common;
    using System.Data;
    using IBM.Data.Db2;

    using (DB2Connection iDB2Conn = new DB2Connection("Server=IP-ADDRESS:446;database=S2151CCW;UserID=Userid;Password=password;"))
    using (DB2Command iDB2Cmd = new DB2Command())
    {

        iDB2Conn.Open();
        iDB2Cmd.Connection = iDB2Conn;

    }

    the erorr message is:

    IBM.Data.Db2.DB2Exception
      HResult=0x80004005
      Message=External component has thrown an exception.
      Source=IBM.Data.Db2
      StackTrace:
       at IBM.Data.Db2.DB2ConnPool.Open(DB2Connection connection, String& szConnectionString, DB2ConnSettings& ppSettings, Object& ppConn)
       at IBM.Data.Db2.DB2Connection.Open()
       at Program.<Main>$(String[] args) in C:\TestProject\ApiTest\ApiTest\Program.cs:line 13

    Do you have any advice?

    i appreciate any help

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 6.  RE: Connect from .NET6

    IBM Champion
    Posted Tue June 27, 2023 10:31 AM

    Hey Bjorn,

    To connect to an IBM Db2 database from a .NET 6 application, you can use the IBM.Data.DB2.Core package, which provides the necessary functionality for establishing a connection and interacting with the database. Here's a sample code to guide you on how to connect to Db2 from .NET :

    using System; using IBM.Data.DB2.Core; namespace Db2ConnectionExample { class Program { static void Main(string[] args) { // Step 1: Define the connection string string connectionString = "Server=myServerAddress;Database=myDatabase;UID=myUsername;PWD=myPassword;"; try { // Step 2: Create a Db2Connection object using (Db2Connection connection = new Db2Connection(connectionString)) { // Step 3: Open the connection connection.Open(); // Step 4: Interact with the database using (Db2Command command = connection.CreateCommand()) { // Step 5: Execute a SQL query command.CommandText = "SELECT * FROM your_table"; using (Db2DataReader reader = command.ExecuteReader()) { // Step 6: Retrieve and process the data while (reader.Read()) { // Access the retrieved data int id = reader.GetInt32(0); string name = reader.GetString(1); Console.WriteLine($"ID: {id}, Name: {name}"); } } } } } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } }


    I hope this helps you !



    ------------------------------
    Youssef Sbai Idrissi
    Software Engineer
    ------------------------------



  • 7.  RE: Connect from .NET6

    Posted Tue June 27, 2023 12:41 PM

    Hi Youssef,

    I tried the code you pasted here and seems it didn't work.

    here is what i did.

    i created a console application with Visual Studio 2022 (c sharp) on .NET 6. I installed IBM.Data.DB2.Core (V3.1.0.600) from NuGet packages.

    i copied your code to my application and change the connection string. Here is my code.

    using System;
    using IBM.Data.DB2.Core;

    // Step 1: Define the connection string
    string connectionString = "Server=IP_ADDRESS;Database=DatabaseName;UID=xxxxxx;PWD=xxxxxx;";

    try
    {
        // Step 2: Create a Db2Connection object
        using (DB2Connection connection = new DB2Connection(connectionString))
        {
            // Step 3: Open the connection
            connection.Open();

            // Step 4: Interact with the database
            using (DB2Command command = connection.CreateCommand())
            {
                // Step 5: Execute a SQL query
                command.CommandText = "SELECT * FROM your_table";

                using (DB2DataReader reader = command.ExecuteReader())
                {
                    // Step 6: Retrieve and process the data
                    while (reader.Read())
                    {
                        // Access the retrieved data
                        int id = reader.GetInt32(0);
                        string name = reader.GetString(1);

                        Console.WriteLine($"ID: {id}, Name: {name}");
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: " + ex.Message);
    }

    Please note:

    1. I didn't change your code from Step 4 as it stopped at step 3.
    2. In your code, the Db2Connection and Db2Command might be typo. Seems they should be DB2Connection and DB2Command

    Here is the error message:

    IBM.Data.DB2.Core.DB2Exception
      HResult=0x80004005
      Message=ERROR [08001] [IBM] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "192.168.1.239".  Communication function detecting the error: "connect".  Protocol specific error code(s): "10061", "*", "*".  SQLSTATE=08001

      Source=IBM.Data.DB2.Core
      StackTrace:
       at IBM.Data.DB2.Core.DB2ConnPool.Open(DB2Connection connection, String& szConnectionString, DB2ConnSettings& ppSettings, Object& ppConn)
       at IBM.Data.DB2.Core.DB2Connection.Open()
       at Program.<Main>$(String[] args) in C:\TestProject\ApiTest\ApiTest\Program.cs:line 13

      This exception was originally thrown at this call stack:
        [External Code]
        Program.<Main>$(string[]) in Program.cs

    Can you please help with this?

    by the way, is there any license needed to make it work?

    Thank you so much,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 8.  RE: Connect from .NET6

    IBM Champion
    Posted Wed June 28, 2023 02:43 AM

    10061 error code means ECONNREFUSED.

    Check connectivity to the server, IP, port, authorisation etc.

    For SQLCODE 30081 see this: https://www.ibm.com/docs/en/db2/11.5?topic=errors-sql30081n-tcpip-communication



    ------------------------------
    Jan Nelken
    ------------------------------



  • 9.  RE: Connect from .NET6

    Posted Wed June 28, 2023 03:55 PM

    Hi Jan,

    I can confirm that, the server IP and database name are correct. the user and password are correct.

    The site you referred doesn't mention how to fix it on IBM i server.

    Thank you anyway for your help,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 10.  RE: Connect from .NET6

    Posted Tue June 27, 2023 12:45 PM

    Hi Youssef,

    I might need to clarify that my DB2 server is IBM i. OS version is V7.4

    I appreciate any help.

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 11.  RE: Connect from .NET6

    IBM Champion
    Posted Wed June 28, 2023 08:43 AM

    Hey bruce,

    that's an important clarification.

    You need to make some adjustments for the code I showed before, for IBM i we use IBM.Data.DB2 instead of IBM.Data.DB2.core, and that changes some things, I hope this goes smoothly for you :

    using System; using IBM.Data.DB2; namespace Db2ConnectionExample { class Program { static void Main(string[] args) { // Step 1: Define the connection string string connectionString = "DataSource=myServerAddress;UserID=myUsername;Password=myPassword;"; try { // Step 2: Create a Db2Connection object using (DB2Connection connection = new DB2Connection(connectionString)) { // Step 3: Open the connection connection.Open(); // Step 4: Interact with the database using (DB2Command command = connection.CreateCommand()) { // Step 5: Execute a SQL query command.CommandText = "SELECT * FROM your_table"; using (DB2DataReader reader = command.ExecuteReader()) { // Step 6: Retrieve and process the data while (reader.Read()) { // Access the retrieved data int id = reader.GetInt32(0); string name = reader.GetString(1); Console.WriteLine($"ID: {id}, Name: {name}"); } } } } } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } } }

     



    ------------------------------
    Youssef Sbai Idrissi
    Software Engineer
    ------------------------------



  • 12.  RE: Connect from .NET6

    Posted Wed June 28, 2023 03:12 PM

    Hi Youssef,

    i deleted the package IBM.Data.DB2.Core and installed the package Net.IBM.Data.Db2 (7.0.0.200, i assume you meant this package because I couldn't find package IBM.Data.DB2).

    In code section, except using part, only the connection string was changed. i use the connection string "DataSource=192.168.1.239;UserID=MyUserId;Password=MyPassword;". The program errored out at using (DB2Connection connection = new DB2Connection(connectionString))

    The error message is:

    System.ArgumentException
      HResult=0x80070057
      Message=Invalid argument
      Source=IBM.Data.Db2
      StackTrace:
       at IBM.Data.Db2.DB2ConnPool.ReplaceConnectionStringParms(DB2Connection connection, String szValue, DB2ConnSettings& pSettings, DB2ConnSettingsInternal& pSettingsInternal, Boolean bAttach, Boolean pushDownStrAppended)
       at IBM.Data.Db2.DB2Connection.set_ConnectionString(String value)
       at IBM.Data.Db2.DB2Connection..ctor(String connectionString)
       at Program.<Main>$(String[] args) in C:\TestProject\ApiTest\ApiTest\Program.cs:line 11

      This exception was originally thrown at this call stack:
        [External Code]
        Program.<Main>$(string[]) in Program.cs

    if i use the connection string "Server=192.168.1.239;Database=S2151CCW;UID=MyUserID;PWD=MyPassword;", the program errored out at connection.Open()

    The error message is:

    IBM.Data.Db2.DB2Exception
      HResult=0x80004005
      Message=External component has thrown an exception.
      Source=IBM.Data.Db2
      StackTrace:
       at IBM.Data.Db2.DB2ConnPool.Open(DB2Connection connection, String& szConnectionString, DB2ConnSettings& ppSettings, Object& ppConn)
       at IBM.Data.Db2.DB2Connection.Open()
       at Program.<Main>$(String[] args) in C:\TestProject\ApiTest\ApiTest\Program.cs:line 14

      This exception was originally thrown at this call stack:
        [External Code]
        Program.<Main>$(string[]) in Program.cs

    Another question is: does it need db2 connection license? if yes, how can i get the license (better for a temporary license because i want to try first of all)?

    Can you please help me with this?

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 13.  RE: Connect from .NET6

    IBM Champion
    Posted Wed June 28, 2023 04:58 PM

    Are you sure that your IBM I-series server is on the same network? You used 192.168.1.239 as IP address of database server ...



    ------------------------------
    Jan Nelken
    ------------------------------



  • 14.  RE: Connect from .NET6

    Posted Thu June 29, 2023 12:01 PM

    Hi Jan,

    Yes, IBM i and my development PC are on the same network 192.168.1.0.

    Thanks,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 15.  RE: Connect from .NET6

    IBM Champion
    Posted Thu June 29, 2023 03:37 PM

    I am making a far stretching assumptions :-)

    IP address of I-series server is correct;
    port number of I-series server is correct;
    There is a listener on I series which is actively listening on IP Address:port as described above;
    There is no firewall rules blocking access to above mentione ip:port.

    If you double checked and verified all that (and telnet session to ip:port appears to be connecting) then you may explore applying Db2 Connect license.
    Perhaps this be of any assistance:
    https://www.ibm.com/docs/en/db2/11.5?topic=li-activating-license-certificate-file-db2-connect-unlimited-edition



    ------------------------------
    Jan Nelken
    ------------------------------



  • 16.  RE: Connect from .NET6

    Posted Fri June 30, 2023 12:45 PM

    Hi Jan,

    I didn't figure out how to download the license for my DB2 for i.

    So far the error message of the connection string is not about the license issue. If it can go further to complain about the license issue. I will check this back.

    Thank you so much,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 17.  RE: Connect from .NET6

    IBM Champion
    Posted Thu June 29, 2023 03:41 PM
    Edited by Youssef Sbai Idrissi Thu June 29, 2023 03:48 PM

    Hey bruce, i'm sorry that you didn't resolve your issue until now. I know how frustrating that can be.
    For IBM.Data.DB2 i was talking about this one : https://www.ibm.com/docs/en/db2/11.1?topic=namespaces-ibmdatadb2
    But it's okay if you can't find it, you can use this one instead : https://www.nuget.org/packages/IBM.Data.DB2.iSeries/

    And before you connect, make sure the port is open and isnt occupied by any other process. 
    Use this code now and let me know what it gives you ( use User ID with a space,  ) :

    using System;
    using IBM.Data.DB2.iSeries;

    namespace Db2ConnectionExample
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Step 1: Define the connection string
              string connectionString = "Data Source=IPAddress;User ID=Username;Password=Password;Initial Catalog=DatabaseName;Port=PortNumber;";

                try
                {
                    // Step 2: Create an iDB2Connection object
                    using (iDB2Connection connection = new iDB2Connection(connectionString))
                    {
                        // Step 3: Open the connection
                        connection.Open();

                        // Step 4: Interact with the database
                        using (iDB2Command command = connection.CreateCommand())
                        {
                            // Step 5: Execute a SQL query
                            command.CommandText = "SELECT * FROM your_table";

                            using (iDB2DataReader reader = command.ExecuteReader())
                            {
                                // Step 6: Retrieve and process the data
                                while (reader.Read())
                                {
                                    // Access the retrieved data
                                    int id = reader.GetInt32(0);
                                    string name = reader.GetString(1);

                                    Console.WriteLine($"ID: {id}, Name: {name}");
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred: " + ex.Message);
                }
            }
        }
    }

     Let me know if there is anything else i can help with



  • 18.  RE: Connect from .NET6

    Posted Fri June 30, 2023 12:06 PM

    Hi Youssef,

    I didn't mention that my application works perfectly with IBM.Data.DB2.iSeries (included in IBM i Access Client Solution) on .NET 4.8 Framework on Windows server 2016. I want to migrate the application to a Linux server. That's why I want to re-write the code on .NET 6 or 7. On the .NET 6 or 7, I cannot use IBM.Data.DB2.iSeries anymore because it depends on .NET Framework.

    The connection string for IBM.Data.DB2.iSeries is:

    DataSource=192.168.1.239;UserID=MyUserID;Password=MyPassword;Naming=sql;LibraryList=A800EP,A800AP;AllowUnsupportedChar=true;

    i am looking for a solution running on .NET 6 or 7. From the link below, seems i have to use Net.IBM.Data.Db2. https://community.ibm.com/community/user/datamanagement/blogs/vishwa-hs1/2020/07/12/db2-net-packages-download-and-configure

    I just couldn't make it work. what kind of connection string I should use?

    Thank you so much for your help,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 19.  RE: Connect from .NET6

    IBM Champion
    Posted Mon July 03, 2023 11:59 AM

    Try getting db2cli validate work first, once  it's working you'll know the problem is in the code

    db2cli validate -connect -dsn XXX -user YYY -passwd ZZZ
    Then once it works, the connection should be in this format :
    "Server=your_server;Database=your_database;UserID=your_user;Password=your_password;"



    ------------------------------
    Youssef Sbai Idrissi
    Software Engineer
    ------------------------------



  • 20.  RE: Connect from .NET6

    Posted Tue July 04, 2023 02:29 PM

    Hi Youssef,

    i am sorry to bother you again and again. this is really out of my knowledge.

    i downloaded v11.5.4_nt32_odbc_cli.zip (IBM Data Server Driver for ODBC and CLI
    (CLI Driver)) and unpacked it to folder C:\Software, but i don't know how to correctly configure files db2cli.ini and db2dsdriver.cfg. So the testing is failed.

    error message:

    [FAILED]: [IBM][CLI Driver] SQL1531N  The connection failed because the name specified with the DSN connection string keyword could not be found in either the db2dsdriver.cfg configuration file or the db2cli.ini configuration file.  Data source name specified in the connection string: "MYTEST".

    this is really frustrating.

    don't know what else i can do about this. who should i ask for the help.

    Thank you,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 21.  RE: Connect from .NET6

    IBM Champion
    Posted Tue July 04, 2023 09:17 PM

    It's pointing to MYTEST now, which is better.
    MYTEST should be referenced in your db2dsdriver.cfg, add it as a database entry and retry. 

    I believe if it really doesn't work, some engineer or IBM support should connect with you and have a look at your environment.



    ------------------------------
    Youssef Sbai Idrissi
    Software Engineer
    ------------------------------



  • 22.  RE: Connect from .NET6

    Posted Wed July 05, 2023 02:17 PM

    Hi Youssef,

    As i said, i really don't know how to configure db2dsdriver.cfg file.

    Thank you anyway,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 23.  RE: Connect from .NET6

    Posted Thu June 29, 2023 08:11 AM

    10061 is a network error.  The most likely reason is that you have the wrong port number.



    ------------------------------
    Tommy Petersen
    ------------------------------



  • 24.  RE: Connect from .NET6

    Posted Thu June 29, 2023 03:28 PM

    As someone else suggested, also ensure you have the correct IP address, if the address exists and there is no Db2 subsystem you might get the same error.



    ------------------------------
    Tommy Petersen
    ------------------------------



  • 25.  RE: Connect from .NET6

    Posted Fri June 30, 2023 05:19 PM

    Hi Tommy,

    i didn't specify the port in the connection string because i don't how to determine which port to use.

    on my current application developed with IBM.Data.DB2.iSeries (installed from IBM i Access Client Solution) on .NET 4.8 Framework, i didn't specify the port.

    it works perfectly.

    Thank you,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 26.  RE: Connect from .NET6

    IBM Champion
    Posted Fri June 30, 2023 06:15 PM
    Edited by Jan Nelken Fri June 30, 2023 06:19 PM

    On an IBM I-series - usual port is 446.

    To display port number :

    1. log in using your terminal TN5250 emulator
    2. type WRKSRVTBLE and search a line like:
        drda                                      xxxx  tcp  

    where xxxx is the portnumber : e.g. 446

    In order to determine the local Relational Data Base name, on the iSeries, issue the command:    WRKRDBDIRE

    1. log in using your terminal TN5250 emulator
    2. type the command WRKRDBDIRE to display the Location name 'Entry'



    ------------------------------
    Jan Nelken
    ------------------------------



  • 27.  RE: Connect from .NET6

    Posted Tue July 04, 2023 02:31 PM

    Hi Jan,

    i tested it with the port and database name and it didn't work.

    Thank you,

    Bruce



    ------------------------------
    Bruce Ma
    ------------------------------



  • 28.  RE: Connect from .NET6

    Posted Mon November 13, 2023 11:52 AM

    Hello Bruce Ma 

    Were you able to connect to the DB2 data base with .net 6?

    Thanks

    Mohamed Ali



    ------------------------------
    mohamed ali medali ben amor
    ------------------------------



  • 29.  RE: Connect from .NET6

    Posted Tue January 30, 2024 03:34 PM

    Hi Bruce Ma

    Did you manage to make the connection with .net 6 and DB2 for i?

    I have the same problem you mention and we cannot connect



    ------------------------------
    MARIA RUIZ
    ------------------------------



  • 30.  RE: Connect from .NET6

    Posted Tue January 30, 2024 10:30 PM
    Hi Maria,

    i used IBM toolbox for java to connect to IBM i and it is free and worked very well.

    Thanks,
    Bruce





  • 31.  RE: Connect from .NET6

    Posted Sat February 03, 2024 06:56 AM

    Hello Bruce MA

    Do you mean that you switched from .Net to Java?

    Thanks



    ------------------------------
    mohamed ali medali ben amor
    ------------------------------