熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

C#連接DB2數據庫的方法和代碼

2022-06-13   來源: ASP編程 

  項目中遇到的net(C#) 連接DB問題解決了和大家分享一下

  下面的程序是同不Sql中的表與DB中的表

  using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemDataOdbc;
using SystemDataSqlClient;
using SystemData;

  namespace DBImport
{
    class Program
    {
        static void Main(string[] args)
        {
            OdbcConnection DBConnection = new OdbcConnection(Dsn=DataSourceName;uid=UserName;pwd=mismidas);//創建DB的數據源連接

  //這裡是用數據源連接的

  //DataSourceName 數據源名稱

  //UserName 用戶名

  //pwd 用戶口令
            //OdbcCommand

  SqlConnection conSql = new SqlConnection(Data Source=oadatabase\\userdb;Initial Catalog=GongChengGuanLi;Persist Security Info=True;User ID=sqlUserName;Password=SqlPassword);//Sql的數據庫連接
            DBConnectionOpen();//打開DB的數據源連接
            SqlDataAdapter ZDAdapter = new SqlDataAdapter(new SqlCommand(Select * from SCD_GXDY_XGJL conSql));
            SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(ZDAdapter);
            DataSet ZDDataSet = new DataSet();
            ZDAdapterFill(ZDDataSet SCD_GXDY_XGJL);
            for (int i = ; i < ZDDataSetTables[SCD_GXDY_XGJL]RowsCount; i++)
            {
                ConsoleWriteLine(importing +i+ );
                DataRow row = ZDDataSetTables[SCD_GXDY_XGJL]Rows[i];
                int KHDM = ;
                try
                {
                    KHDM = intParse( + row[KHDM]);
                }
                catch (Exception paresException)
                {
                    KHDM = ;
                }

  //KHDM
                if ( + row[KHDM] != && KHDM!=)
                {
                    OdbcCommand cmdSelect = new OdbcCommand(Select CCUSTCNMECTYPECLOCCMDPFX from VtstFRCM where CCUST= +KHDM+ DBConnection);
                   OdbcDataReader odbcReader= cmdSelectExecuteReader();
                   if (odbcReaderRead())//從DB中讀取數據
                   {
                       row[CNME] = +odbcReader[CNME];
                       row[CTYPE] = + odbcReader[CTYPE];
                       row[CLOC] = + odbcReader[CLOC];
                       row[CMDPFX] = + odbcReader[CMDPFX];
                   }
                   odbcReaderClose();
                }

  }
            ConsoleWriteLine(Updating );
            ZDAdapterUpdate(ZDDataSet SCD_GXDY_XGJL);
            ConsoleWriteLine(Commplete!);
            ConsoleWriteLine(Press any Key to Exit!);
            ConsoleRead();

  }
    }
}

  但可能有朋友要問了DB的數據源是怎麼建立的安裝IBM DB RUNTime Client

  然後運行從屬站架構輔助程式來添加數據源(剩下就是下一步了啊)

  


From:http://tw.wingwit.com/Article/program/ASP/201311/21832.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.