❶ 如何在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&brvbar;&brvbar;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&brvbar;&brvbar;retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
if(retcode==SQL_SUCCESS&brvbar;&brvbar;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&brvbar;&brvbar;retcode==SQL_SUCCESS_WITH_INFO)
{
retcode=SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
if(retcode==SQL_SUCCESS&brvbar;&brvbar;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]='';
i++;
iPos+=i;
for(i=0;i<strlen(Server);i++)
{
Attr[iPos+i]=Server[i];
}
iPos+=i;
i++;
for(i=0;i<EdtServerName->Text.Length();i++)
{
Attr[iPos+i]=EdtServerName->Text[i+1];
}
Attr[iPos+i]='';
i++;
iPos+=i;
for(i=0;i<strlen(Database);i++)
{
Attr[iPos+i]=Database[i];
}
Attr[iPos+i]='';
i++;
Attr[iPos+i]='';
SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,"SQLServer",Attr);
TRegistry*pRegInfo=newTRegistry();
pRegInfo->RootKey=HKEY_LOCAL_MACHINE;
pRegInfo->OpenKey("\Software\VividView\Standard",true);
pRegInfo->WriteString("User",EdtSa->Text);
pRegInfo->WriteString("Pass",EdtPassword->Text);
pRegInfo->CloseKey();
deletepRegInfo;
MessageBox(this->Handle,"系统配置已经成功更新,ODBC数据源建立成功",Caption.c_str(),MB_OK+MB_ICONINFORMATION+MB_APPLMODAL);
Close();
}
catch(...)
{
MessageBox(this->Handle,"系统配置失败,请重试。",Caption.c_str(),MB_OK+MB_ICONWARNING+MB_APPLMODAL);
}
}
//---------------------------------------------------------------------------

void__fastcallTFrmSetODBC::EdtServerNameKeyPress(TObject*Sender,char&Key)
{
if(Key==VK_RETURN)
{
SendMessage(this->Handle,WM_NEXTDLGCTL,0,0);
Key=0;
}
}
//---------------------------------------------------------------------------

void__fastcallTFrmSetODBC::btnCreateClick(TObject*Sender)
{
if(EdtServerName->Text=="")
{
MessageBox(NULL,"请输入SQLServer服务器名","参数错误",MB_OK+MB_ICONEXCLAMATION);
return;
}
if(EdtSa->Text=="")
{
MessageBox(NULL,"请输入SQLServer管理员账号","参数错误",MB_OK+MB_ICONEXCLAMATION);
return;
}
ConfigIt();
}
//---------------------------------------------------------------------------

void__fastcallTFrmSetODBC::btnCancelClick(TObject*Sender)
{
Close();
}
//---------------------------------------------------------------------------