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)