C++Builder6如何创基本信息表
展开全部
C++Builder 6 数据库连接配置
配置文件名:config.ini
配置文件内容:
*******开始********
[connection]
User=sa(用户名)
Password=密码
Server=服务器名
Database=数据库名
*******结束***********
Unit_DM.h文件内容里加入
#include
全文内容如下:
[@more@]
//---------------------------------------------------------------------------
#ifndef Unit_DMH
#define Unit_DMH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include //增加 的头文件
//---------------------------------------------------------------------------
class TDM : public TDataModule
{
__published: // IDE-managed Components
TADOConnection *ADOConn;
TADOQuery *Qry_Dept;
TDataSource *DS_Dept;
TStringField *Qry_DeptdeptNo;
TStringField *Qry_DeptdeptName;
TStringField *Qry_DeptdeptState;
TStringField *Qry_DeptdeptMemo;
void __fastcall DataModuleCreate(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TDM(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TDM *DM;
//---------------------------------------------------------------------------
#endif
************************************
Unit_dm.cpp文件内容 如下 :
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit_DM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDM *DM;
//---------------------------------------------------------------------------
__fastcall TDM::TDM(TComponent* Owner)
: TDataModule(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TDM::DataModuleCreate(TObject *Sender)
{
String connStr="",PasswordStr="",UserStr="",DatabaseStr="",ServerStr=""; //
TIniFile *ini;
try
{
// connStr= " Provider=SQLOLEDB.1;Password=wzr;Persist Security Info=
// True;User ID=sa;Initial Catalog=数据库名;Data Source=服务器名 "
ADOConn->Close();
ini=new TIniFile(".//config.ini");
PasswordStr=ini->ReadString("connection","Password","") ;
// ShowMessage(PasswordStr);
UserStr=ini->ReadString("connection","User","");
// ShowMessage(UserStr);
DatabaseStr=ini->ReadString("connection","Database","");
ServerStr=ini->ReadString("connection","Server","");
connStr="Provider=SQLOLEDB.1;Password="+PasswordStr+";Persist Security Info=True;User ID=";
connStr=connStr+UserStr+";Initial Catalog="+DatabaseStr+";Data Source="+ServerStr ;
ADOConn->ConnectionString="";
ADOConn->ConnectionString=connStr;
ADOConn->Open();
ADOConn->Connected=true;
}
catch(...)
{
Application->MessageBoxA("数据库连接失败,请检查数据库服务或网络连接。","错误信息",0+64);
}
}
配置文件名:config.ini
配置文件内容:
*******开始********
[connection]
User=sa(用户名)
Password=密码
Server=服务器名
Database=数据库名
*******结束***********
Unit_DM.h文件内容里加入
#include
全文内容如下:
[@more@]
//---------------------------------------------------------------------------
#ifndef Unit_DMH
#define Unit_DMH
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include //增加 的头文件
//---------------------------------------------------------------------------
class TDM : public TDataModule
{
__published: // IDE-managed Components
TADOConnection *ADOConn;
TADOQuery *Qry_Dept;
TDataSource *DS_Dept;
TStringField *Qry_DeptdeptNo;
TStringField *Qry_DeptdeptName;
TStringField *Qry_DeptdeptState;
TStringField *Qry_DeptdeptMemo;
void __fastcall DataModuleCreate(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TDM(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TDM *DM;
//---------------------------------------------------------------------------
#endif
************************************
Unit_dm.cpp文件内容 如下 :
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit_DM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TDM *DM;
//---------------------------------------------------------------------------
__fastcall TDM::TDM(TComponent* Owner)
: TDataModule(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TDM::DataModuleCreate(TObject *Sender)
{
String connStr="",PasswordStr="",UserStr="",DatabaseStr="",ServerStr=""; //
TIniFile *ini;
try
{
// connStr= " Provider=SQLOLEDB.1;Password=wzr;Persist Security Info=
// True;User ID=sa;Initial Catalog=数据库名;Data Source=服务器名 "
ADOConn->Close();
ini=new TIniFile(".//config.ini");
PasswordStr=ini->ReadString("connection","Password","") ;
// ShowMessage(PasswordStr);
UserStr=ini->ReadString("connection","User","");
// ShowMessage(UserStr);
DatabaseStr=ini->ReadString("connection","Database","");
ServerStr=ini->ReadString("connection","Server","");
connStr="Provider=SQLOLEDB.1;Password="+PasswordStr+";Persist Security Info=True;User ID=";
connStr=connStr+UserStr+";Initial Catalog="+DatabaseStr+";Data Source="+ServerStr ;
ADOConn->ConnectionString="";
ADOConn->ConnectionString=connStr;
ADOConn->Open();
ADOConn->Connected=true;
}
catch(...)
{
Application->MessageBoxA("数据库连接失败,请检查数据库服务或网络连接。","错误信息",0+64);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询