项目中遇到的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
然后运行从属站架构辅助程式来添加数据源(剩下就是下一步了啊)