bcb编程
❶ 如何在BCB中编程实现配置ODBC
//---------------------------------------------------------------------------
#include<vcl.h>
#pragmahdrstop
#defineWIN32
#include<ODBCINST.H>
#include<SQLext.h>
#include"ConfigCondition.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TForm1*Form1;
//---------------------------------------------------------------------------
__fastcallTForm1::TForm1(TComponent*Owner)
:TForm(Owner)
{}
//---------------------------------------------------------------------------
voidTForm1::Config(void)
{
SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,
"MicrosoftAccessDriver(*.mdb) ",
"DSN=JCY "
"DefaultDir=C:\InetPub\WWWROOT\JCY\Data\DB "
"DriverID=25 "
"DBQ=C:\InetPub\WWWROOT\JCY\Data\DB\JCY.mdb "
);
charUnitName[255];
charInstallSerial[255];
charUserPhone[255];
charUserName[255];
charWindowPath[255];
GetWindowsDirectory(WindowPath,255);
StrCat(WindowPath,"\JCY.INI");
GetPrivateProfileString("INFOR","UNITNAME","",UnitName,255,WindowPath);
GetPrivateProfileString("INFOR","SERIAL","",InstallSerial,255,WindowPath);
GetPrivateProfileString("INFOR","USERPHONE","",UserPhone,255,WindowPath);
GetPrivateProfileString("INFOR","USERNAME","",UserName,255,WindowPath);
SQLHENVhenv;
SQLHDBChdbc;
SQLRETURNretcode;
SQLHSTMThstmt;
retcode=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
if(retcode==SQL_SUCCESS¦¦retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
if(retcode==SQL_SUCCESS¦¦retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
if(retcode==SQL_SUCCESS¦¦retcode==SQL_SUCCESS_WITH_INFO)
{
/*Setlogintimeoutto5seconds.*/
SQLSetConnectAttr(hdbc,SQL_LOGIN_TIMEOUT,"5",1);
retcode=SQLConnect(hdbc,(SQLCHAR*)"JCY",SQL_NTS,
(SQLCHAR*)"Admin",SQL_NTS,
(SQLCHAR*)"",0);
if(retcode==SQL_SUCCESS¦¦retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
if(retcode==SQL_SUCCESS¦¦retcode==SQL_SUCCESS_WITH_INFO)
{
charSQLString[1024];
StrCopy(SQLString,"UpdateSystemSetUnitName='");
StrCat(SQLString,UnitName);
StrCat(SQLString,"',ProctSerial='");
StrCat(SQLString,InstallSerial);
StrCat(SQLString,"',UserName='");
StrCat(SQLString,UserName);
StrCat(SQLString,"',UserPhone='");
StrCat(SQLString,UserPhone);
StrCat(SQLString,"'");
SQLExecDirect(hstmt,(SQLCHAR*)SQLString,(SQLINTEGER)StrLen(SQLString));
SQLFreeHandle(SQL_HANDLE_STMT,hstmt);
}
SQLDisconnect(hdbc);
}
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
}
}
SQLFreeHandle(SQL_HANDLE_ENV,henv);
}
//TODO:Addyoursourcecodehere
}
下面的程序则是生成一个ODBC数据源,根据用户的实际配置来生成
//---------------------------------------------------------------------------
#pragmahdrstop
#defineWIN32
#include<vcl.h>
#include<ODBCINST.H>
#include<SQLext.h>
#include"Config.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
#pragmaresource"*.dfm"
TFrmSetODBC*FrmSetODBC;
//---------------------------------------------------------------------------
__fastcallTFrmSetODBC::TFrmSetODBC(TComponent*Owner)
:TForm(Owner)
{
}
//---------------------------------------------------------------------------
void__fastcallTFrmSetODBC::ConfigIt(void)
{
try
{
charAttr[1024];
charDsn[]="DSN=Standard";
charServer[]="SERVER=";
charDatabase[]="DATABASE=Standard";
intiPos=0;
inti=0;
for(i=0;i<strlen(Dsn);i++)
{
Attr[iPos+i]=Dsn[i];
}
Attr[iPos+i]='