Does .Net Framework 4.0 have features for connect

2019-01-22 01:08发布

问题:

I need to know is there any feature of connecting to DB2 database from .net in .Net framework 4.0

EDIT:- I like to know if there is any DB2 provider

回答1:

Yep, the family of IBM.Data.DB2 drivers (collectively found in IBM.Data.DB2.dll, I believe) should work just fine with .NET, providing you install the drivers on your development machine.

Additionally, I was able to get it to work successfully with VS2010Beta and EF4Beta2, despite the lack of Visual Studio Add-ins for VS2010 (as of this date.) If the drivers are installed on your machine already, you just need to add an entry for it in the machine.config file for .NET 4.0's clr.

EDIT: Sample machine.config markup follows. Originally, the config file just had the single SQL server DB Provider Factory entry. Assuming you have IBM.Data.DB2 installed on your machine, you can do what I did and simply open up your 2.0's machine.config file and copy/paste the entries for DB2. Full disclosure, I honestly don't know if all 4 are required, but a clean install of 9.7fp1 inserted all four entries in my 2.0 machine.config, so I went ahead and copied them all over to 4.0 machine.config. Copy/paste, save the file, and restart visual studio 2010 and you should be able to reference the provider in your EDMX with the information in the storage model definition:

<edmx:StorageModels>
  <Schema xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl" Namespace="BlahModel.Store" Alias="Self" Provider="IBM.Data.DB2" ProviderManifestToken="IDS/UNIX64, 11.50.0000">

Note that I'm interested in connecting to an informix database, hence the ProviderManifestToken value. However, I don't think that's required verbatim.

The snippet from my 4.0 machine.config:

<system.data>
    <DbProviderFactories>
        <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
        <add name="IBM DB2 .NET Data Provider" invariant="IBM.Data.DB2" description="IBM DB2 Data Provider for .NET Framework 2.0" type="IBM.Data.DB2.DB2Factory, IBM.Data.DB2, Version=9.0.0.2, Culture=neutral, PublicKeyToken=7c307b91aa13d208" />
        <add name="IBM Informix .NET Data Provider" invariant="IBM.Data.Informix" description="IBM Informix Data Provider for .NET Framework 2.0" type="IBM.Data.Informix.IfxFactory, IBM.Data.Informix, Version=9.0.0.2, Culture=neutral, PublicKeyToken=7c307b91aa13d208" />
        <add name="IBM DB2 .NET Data Provider 9.7.1" invariant="IBM.Data.DB2.9.7.1" description="IBM DB2 Data Provider 9.7.1 for .NET Framework 2.0" type="IBM.Data.DB2.DB2Factory, IBM.Data.DB2.9.7.1, Version=9.7.1.2, Culture=neutral, PublicKeyToken=7c307b91aa13d208" />
        <add name="IBM Informix .NET Data Provider 9.7.1" invariant="IBM.Data.Informix.9.7.1" description="IBM Informix Data Provider 9.7.1 for .NET Framework 2.0" type="IBM.Data.Informix.IfxFactory, IBM.Data.Informix.9.7.1, Version=9.7.1.2, Culture=neutral, PublicKeyToken=7c307b91aa13d208" />          
    </DbProviderFactories>
</system.data>

EDIT 2: IBM's latest DB2 drivers -- v9.7fp4 -- have greatly improved .NET 4.0 and VS2010 Add-In support. Its installation will automatically handle the 4.0 machine.config DbProviderFactories entries. If you previously manually edited entries, as described above, you'll want to comment out/delete them as part of your v9.7fp3 (and earlier) uninstalls.



回答2:

What do you mean? You want something specific to .NET 4.0 or you want to know that whether we can connect with DB2 using .NET Framework or not.

If later is the case, yes you can. OleDBConnection class(which is available in .NET 2.0 and .NET 3.5 as well) has a property ConnectionString in which you set the provider's details. You simply have to give the provider's connection string for your DB2 provider and you should be OK.



回答3:

I am also trying to use Visual Studio 2010 to connect with DB2 via Entity Framework. I tried what kdawg did: I installed the IBM Data Server Driver Package I installed the IBM Visual Studio 2008 addins

I then attempted to create an ADO.NET Entity Data Model in my 2008 project and then convert it to 2010 but the connection to the database wouldn't work. I receive the following error: ERROR [42968] [IBM] SQL1598N An attempt to connect to the database server failed because of a licensing problem. SQLSTATE=42968

Apparently, according to this forum , IBM does not provide the driver for free. You must have DB2 Connect installed and licensed. This is about $12,000.

I decided to try something else.



回答4:

Also you can decompile lib with ildasm.exe to il.il

Then compile using SDK 7 ilasm.exe /RESOURCE=...\IL.res ...\il.il /OUTPUT=...\Ibm.Data.Db2.Net4.dll /DLL



回答5:

I was also searching to download the drivers for connecting DB2. Here is list of all client drivers of DB2 with their versions.

Information for connecting to DB2 is available here. In case the link might be dead in future, So I am posting the sample code here.

using System;
using IBM.Data.DB2;

namespace dotNetSSLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DB2Command MyDB2Command = null;
            // Use the dsn alias that you defined in db2dsdriver.cfg with the db2cli writecfg command in step 1.
            String MyDb2ConnectionString = "database=alias;uid=userid;pwd=password;"; 
            DB2Connection MyDb2Connection = new DB2Connection(MyDb2ConnectionString);
            MyDb2Connection.Open();
            MyDB2Command = MyDb2Connection.CreateCommand();
            MyDB2Command.CommandText = "SELECT branch_code, city from GOSALES.BRANCH";
            Console.WriteLine(MyDB2Command.CommandText);

            DB2DataReader MyDb2DataReader = null;
            MyDb2DataReader = MyDB2Command.ExecuteReader();
            Console.WriteLine("BRANCH\tCITY");
            Console.WriteLine("============================");
            while (MyDb2DataReader.Read())
            {
                for (int i = 0; i <= 1; i++)
                {
                    try
                    {
                        if (MyDb2DataReader.IsDBNull(i))
                        {
                            Console.Write("NULL");
                        }
                        else
                        {
                            Console.Write(MyDb2DataReader.GetString(i));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Write(e.ToString());
                    }
                    Console.Write("\t"); 

                }
                Console.WriteLine("");
            }
            MyDb2DataReader.Close();
            MyDB2Command.Dispose();
            MyDb2Connection.Close();
        }
    }
}

In case anyone is not able to find the DB2 database server for trial then here is list of free trials. Also the Developer Community Edition is free for unlimited use.



标签: .net .net-4.0