//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #define WIN32 #include <ODBCINST.H> #include <SQLext.h> #include "ConfigCondition.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void TForm1::Config(void) { SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN, "Microsoft Access Driver (*.mdb)/0", "DSN=JCY/0" "DefaultDir=C://InetPub//WWWROOT//JCY//Data//DB/0" "DriverID=25/0" "DBQ=C://InetPub//WWWROOT//JCY//Data//DB//JCY.mdb/0/0" ); char UnitName[255]; char InstallSerial[255]; char UserPhone[255]; char UserName[255]; char WindowPath[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); SQLHENV henv; SQLHDBC hdbc; SQLRETURN retcode; SQLHSTMT hstmt; 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) { /* Set login timeout to 5 seconds. */ 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) { char SQLString[1024]; StrCopy(SQLString,"Update System Set UnitName='"); StrCat(SQLString,UnitName); StrCat(SQLString,"',ProductSerial='"); 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: Add your source code here } 下面的程序则是生成一个ODBC数据源,根据用户的实际配置来生成 //--------------------------------------------------------------------------- #pragma hdrstop #define WIN32 #include <vcl.h> #include <ODBCINST.H> #include <SQLext.h> #include "Config.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TFrmSetODBC *FrmSetODBC; //--------------------------------------------------------------------------- __fastcall TFrmSetODBC::TFrmSetODBC(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TFrmSetODBC::ConfigIt(void) { try { char Attr[1024]; char Dsn[]="DSN=Standard"; char Server[]="SERVER="; char Database[]="DATABASE=Standard"; int iPos=0; int i=0; for(i=0;i<strlen(Dsn);i++) { Attr[iPos+i]=Dsn[i]; } Attr[iPos+i]='/0'; 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]='/0'; i++; iPos+=i; for(i=0;i<strlen(Database);i++) { Attr[iPos+i]=Database[i]; } Attr[iPos+i]='/0'; i++; Attr[iPos+i]='/0'; SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,"SQL Server/0",Attr); TRegistry * pRegInfo=new TRegistry(); pRegInfo->RootKey=HKEY_LOCAL_MACHINE; pRegInfo->OpenKey("//Software//Vivid View//Standard",true); pRegInfo->WriteString("User",EdtSa->Text); pRegInfo->WriteString("Pass",EdtPassword->Text); pRegInfo->CloseKey(); delete pRegInfo; 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 __fastcall TFrmSetODBC::EdtServerNameKeyPress(TObject *Sender, char &Key) { if(Key==VK_RETURN) { SendMessage(this->Handle,WM_NEXTDLGCTL,0,0); Key=0; } } //--------------------------------------------------------------------------- void __fastcall TFrmSetODBC::btnCreateClick(TObject *Sender) { if(EdtServerName->Text=="") { MessageBox(NULL,"请输入SQL Server服务器名","参数错误",MB_OK+MB_ICONEXCLAMATION); return; } if(EdtSa->Text=="") { MessageBox(NULL,"请输入SQL Server管理员账号","参数错误",MB_OK+MB_ICONEXCLAMATION); return; } ConfigIt(); } //--------------------------------------------------------------------------- void __fastcall TFrmSetODBC::btnCancelClick(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- |
如何在BCB中编程实现配置ODBC
最新推荐文章于 2016-06-13 18:57:03 发布