动态建立数据库(access mdb)类型数据库 ( 积分: 50 )

  • 主题发起人 主题发起人 bxdwx21
  • 开始时间 开始时间
B

bxdwx21

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在有一事不明,请大家多帮助:就是如何在程序运行
期间动态建立数据库(access mdb类型数据库)然后在动态建表呀

能不能在ADOQuery1,的SQL里写类似create table abc(.......)的语句呀,

下面的我找的,但不对呀,
我菜,用什么控件也要写呀,谢谢


Const
SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'
+'Jet OLEDB:Database Password=%s;';

//=============================================================================
// Procedure: GetTempPathFileName
// Author : ysai
// Date : 2003-01-27
// Arguments: (None)
// Result : string
//=============================================================================
function GetTempPathFileName():string;
//取得临时文件名
var
SPath,SFile:array [0..254] of char;
begin
GetTempPath(254,SPath);
GetTempFileName(SPath,'~SM',0,SFile);
result:=SFile;
DeleteFile(result);
end;

//=============================================================================
// Procedure: CreateAccessFile
// Author : ysai
// Date : 2003-01-27
// Arguments: FileName:String;PassWord:string=''
// Result : boolean
//=============================================================================
function CreateAccessFile(FileName:String;PassWord:string=''):boolean;
//建立Access文件,如果文件存在则失败
var
STempFileName:string;
vCatalog:OleVariant;
begin
STempFileName:=GetTempPathFileName;
try
vCatalog:=CreateOleObject('ADOX.Catalog');
vCatalog.Create(format(SConnectionString,[STempFileName,PassWord]));
result:=CopyFile(PChar(STempFileName),PChar(FileName),True);
DeleteFile(STempFileName);
except
result:=false;
end;
end;


---------------------------------------------------------
 
我现在有一事不明,请大家多帮助:就是如何在程序运行
期间动态建立数据库(access mdb类型数据库)然后在动态建表呀

能不能在ADOQuery1,的SQL里写类似create table abc(.......)的语句呀,

下面的我找的,但不对呀,
我菜,用什么控件也要写呀,谢谢


Const
SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'
+'Jet OLEDB:Database Password=%s;';

//=============================================================================
// Procedure: GetTempPathFileName
// Author : ysai
// Date : 2003-01-27
// Arguments: (None)
// Result : string
//=============================================================================
function GetTempPathFileName():string;
//取得临时文件名
var
SPath,SFile:array [0..254] of char;
begin
GetTempPath(254,SPath);
GetTempFileName(SPath,'~SM',0,SFile);
result:=SFile;
DeleteFile(result);
end;

//=============================================================================
// Procedure: CreateAccessFile
// Author : ysai
// Date : 2003-01-27
// Arguments: FileName:String;PassWord:string=''
// Result : boolean
//=============================================================================
function CreateAccessFile(FileName:String;PassWord:string=''):boolean;
//建立Access文件,如果文件存在则失败
var
STempFileName:string;
vCatalog:OleVariant;
begin
STempFileName:=GetTempPathFileName;
try
vCatalog:=CreateOleObject('ADOX.Catalog');
vCatalog.Create(format(SConnectionString,[STempFileName,PassWord]));
result:=CopyFile(PChar(STempFileName),PChar(FileName),True);
DeleteFile(STempFileName);
except
result:=false;
end;
end;


---------------------------------------------------------
 
// 1: Using ADOX:

uses
ComObj;

// You can with ADOX (Microsoft ADO Extensions for DDL and Security),

function CreateAccessDatabase(FileName: string): string;
var
cat: OLEVariant;
begin
Result := '';
try
cat := CreateOleObject('ADOX.Catalog');
cat.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + FileName + ';');
cat := NULL;
except
on e: Exception do Result := e.message;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
CreateAccessDatabase('c:/Testdb.mdb');
end;


// 2: Using ODBC:

const
ODBC_ADD_DSN = 1; // Add data source
ODBC_CONFIG_DSN = 2; // Configure (edit) data source
ODBC_REMOVE_DSN = 3; // Remove data source
ODBC_ADD_SYS_DSN = 4; // add a system DSN
ODBC_CONFIG_SYS_DSN = 5; // Configure a system DSN
ODBC_REMOVE_SYS_DSN = 6; // remove a system DSN
ODBC_REMOVE_DEFAULT_DSN = 7; // remove the default DSN

function SQLConfigDataSource(hwndParent: HWND;
fRequest: Word;
lpszDriver: LPCSTR;
lpszAttributes: LPCSTR): BOOL; stdcall; external 'ODBCCP32.DLL';

function CreateDB(const Database: string): Boolean;
begin
Result := SQLConfigDataSource(0, ODBC_ADD_DSN,
'Microsoft Access Driver (*.mdb)', PChar('CREATE_DB=' + Database + ' General'#0));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
CreateAccessDatabase('c:/Testdb.mdb');
end;

// 1: Using VBA:

uses
ComObj;

procedure TForm1.Button1Click(Sender: TObject);
var
AccessApp:Variant;
begin
AccessApp:=CreateOleObject('Access.Application');
AccessApp.NewCurrentDatabase('c:/111.mdb');
AccessApp:=Unassigned;
end;
 
http://bbs.sdili.edu.cn/asp/slx/news/read.asp?id=87
 
作者?: gzkiko
标题?: 使用ADOX创建MDB库和表
关键字:
分类?: 个人专区
密级?: 公开
(评分: , 回复: 0, 阅读: 66) »»
CreateAccess:=CreateOleObject('ADOX.Catalog');
CreateAccess.Create('Provider=Microsoft.Jet.OLEDB.3.51;Data Source='+AppPath+'/'+tablename+'.mdb');
CreateAccess.ActiveConnection := ...//活动连接 _Connection 接口
NewTable := CreateOleObject('ADOX.Table');
NewTable.Name = 'NEW_TABLE';
CreateAccess.Tables.Append(NewTable); //创建完毕
 
谢谢 app2001兄,Avalon兄(您的好好全哦,感动),ZYONG兄,谢谢大家,结贴
 
多人接受答案了。
 
后退
顶部