. Net (C #) connection to DB2

Source: Internet
Author: User
Tags ibm db2

The. NET (C #) connection to DB2 problem encountered in the project has been solved. Let's share it with you.

The following program is different from the table in sql2005 and the table in DB2.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data. ODBC;
Using system. Data. sqlclient;
Using system. Data;

Namespace db2import
{
Class Program
{
Static void main (string [] ARGs)
{
Odbcconnection db2connection = new odbcconnection ("DSN = CENAME; uid = username; Pwd = mismidas"); // create a data source connection for DB2

// The data source is used for connection.

// Datasourcename Data Source Name

// Username

// PWD User Password
// Odbccommand

Sqlconnection consql = new sqlconnection ("Data Source = oadatabase // userdb2005; initial catalog = gongchengguanli; persist Security info = true; user id = sqlusername; Password = sqlpassword "); // sql2005 database connection
Db2connection. open (); // open the DB2 data source connection
Sqldataadapter zdadapter = new sqldataadapter (New sqlcommand ("select * From scd_gxdy_xgjl", consql ));
Sqlcommandbuilder mycommandbuilder = new sqlcommandbuilder (zdadapter );
Dataset zddataset = new dataset ();
Zdadapter. Fill (zddataset, "scd_gxdy_xgjl ");
For (INT I = 0; I <zddataset. Tables ["scd_gxdy_xgjl"]. Rows. Count; I ++)
{
Console. writeline ("importing" + I + ".....");
Datarow ROW = zddataset. Tables ["scd_gxdy_xgjl"]. Rows [I];
Int khdm = 0;
Try
{
Khdm = int. parse ("" + row ["khdm"]);
}
Catch (exception paresexception)
{
Khdm = 0;
}

// Khdm
If ("" + row ["khdm"]! = "" & Khdm! = 0)
{
Odbccommand cmdselect = new odbccommand ("select ccust, cnme, ctype, cloc, export pfx from v6tstf. RCM where ccust =" + khdm + "", db2connection );
Odbcdatareader odbcreader = cmdselect. executereader ();
If (odbcreader. Read () // read data from DB2
{
Row ["cnme"] = "" + odbcreader ["cnme"];
Row ["ctype"] = "" + odbcreader ["ctype"];
Row ["cloc"] = "" + odbcreader ["cloc"];
Row ["Export pfx"] = "" + odbcreader ["Export pfx"];
}
Odbcreader. Close ();
}


}
Console. writeline ("Updating ....");
Zdadapter. Update (zddataset, "scd_gxdy_xgjl ");
Console. writeline ("comatrix! ");
Console. writeline ("press any key to exit! ");
Console. Read ();

}
}
}
But some may ask how the DB2 data source was created and how to install the IBM DB2 run-time client.

Then run the "Create component program from the website" to add the data source. (The rest is the next step)

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.