请大家帮帮我吧!(数据库问题)(100分)

  • 主题发起人 主题发起人 lhqnhy
  • 开始时间 开始时间
L

lhqnhy

Unregistered / Unconfirmed
GUEST, unregistred user!
我是刚学Delphi,我在用ADO连接数据时不知如何动态更改数据库路径,请大家帮帮我吧
具体是这样两个问题:
1。如何能在程序动运时显示设置数据库路径的对话框,如左轻候的阅读器中的配置数据库。
2。我的数据库放在程序目录下,求如何动连接。最好有源码。(access)

谢谢
 
认真看左轻候的阅读器源代码就知道了。绝对经典的程序啊
 
uses
SysUtils,Windows;

function GetDBConnectStr(Dbname:string):string;
const
s1='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=';
s2=';Persist Security Info=False' ;
var
path:array[0..MAX_PATH-1] of char;
PathStr:string;
begin
SetString(PathStr,Path,GetModuleFileName(Hinstance,Path,SizeOf(Path)));
Result:=ExtractFilePath(PathStr)+DbName;
Result:=s1+Result+s2;
end;

ADOQuery1.ConnectionString:=GetDBConnectStr('tmp.mdb');
 
//Splash.DFM
object SplashForm: TSplashForm
Left = 308
Top = 134
Cursor = crHourGlass
AutoSize = True
BorderIcons = []
BorderStyle = bsNone
ClientHeight = 308
ClientWidth = 400
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = False
Position = poDesktopCenter
PixelsPerInch = 96
TextHeight = 13
object imgBack: TImage
Left = 0
Top = 0
Width = 400
Height = 308
AutoSize = True
IncrementalDisplay = True
Proportional = True
end
object dlgOpen: TOpenDialog
Filter = 'Microsoft Access 文件 (*.mdb)|*.mdb|所有文件 (*.*)|*.*'
Options = [ofHideReadOnly, ofPathMustExist, ofFileMustExist, ofEnableSizing]
Left = 104
Top = 120
end
end

//------------------------------------------------------------------------------
//Splash.pas,Splash.dfm
//闪现窗体,并联接数据库
//------------------------------------------------------------------------------

unit Splash;

interface

uses
Windows, Messages, SysUtils, Classes, Controls, Forms,
Dialogs, ExtCtrls,Global,ADODB, MainDM, Graphics, StdCtrls;

type
TSplashForm = class(TForm)
dlgOpen: TOpenDialog;
imgBack: TImage;
private
{ Private declarations }
public
{ Public declarations }
end;

function ShowSplashForm:boolean;

var
SplashForm: TSplashForm;

implementation

{$R *.dfm}

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

function ShowSplashForm:boolean;
//显示闪现窗口,同时连接数据库,如果失败则选择其它数据库
function OpenDBFile:boolean;
//选择数据库
begin
result:=true;
SplashForm.dlgopen.FileName:=sDataBaseName;
if SplashForm.dlgopen.Execute then
//sDataBaseName是一个全局变量,为数据库完整路径及名称
sDataBaseName:=SplashForm.dlgopen.FileName
else
result:=false;
Application.ProcessMessages;
end;
begin
Screen.Cursor:=crHourGlass;
result:=true;
SplashForm:=TSplashForm.Create(Application);
SplashForm.Show;
SplashForm.Refresh;
if length(sDataBaseName)=0 then
sDataBaseName:=ExtractFilePath(application.ExeName)
+SDefDataBaseName;//SDefDataBaseName 是默认数据库文件名
if not FileExists(sDataBaseName) then
result:=OpenDBFile;
while result do
try
MainDMForm:=TMainDMForm.Create(Application);
with MainDMForm do
begin
if CNMain.Connected then CNMain.Close;
CNMain.ConnectionString:=format(
SConnectionString,[sDataBaseName,GetPassWord]);
//SConnectionString连接字符串,GetPassWord取得密码函数
CNMain.Open;
end; //with
except
msgbox(SEOpenDBFile,SAppName,mb_iconstop);
//msgbox在共享模块声明,SEOpenDBFile,SAppName是资源字符串
result:=OpenDBFile;
end; //try
if not result then exit;
Application.ProcessMessages;
// Sleep(3000);//延时
Screen.Cursor:=crDefault;
SplashForm.Close;
SplashForm.Free;
end;

end.

//------------------------------------------------------------------------------
//工程文件
//------------------------------------------------------------------------------

program StockManage;

uses
Forms,
Windows,
Messages,
MDIMain in 'MDIMain.pas' {MDIMainForm},
MainDM in 'MainDM.pas' {MainDMForm: TDataModule},
Splash in 'Splash.pas' {SplashForm},
Global in 'Global.pas';

{$R *.res}
var
hMutex,hApp:HWND;
begin
Application.Initialize;
//建立互斥对象
hMutex:=CreateMutex(nil,False,PChar(SAppName));
If GetLastError=ERROR_ALREADY_EXISTS Then
begin
//已经运行,找到句柄
hApp:=FindWindow('TApplication',PChar(SAppName));
//激活
if IsIconic(hApp) then
PostMessage(hApp,WM_SYSCOMMAND,SC_RESTORE,0);
SetForegroundWindow(hApp);
//释放互斥对象
ReleaseMutex(hMutex);
Application.Terminate;
exit;
end;
Application.HintPause :=500;
Application.HintHidePause :=10000;
Application.ShowMainForm :=False;
Application.OnMessage := MDIMainForm.AppMessageHandler;
AddAboutMenu(application.Handle);
//{
LoadSetting;
//显示闪现窗体并检查密码
if not (ShowSplashForm and CheckPassWord) then
begin
ReleaseMutex(hMutex);
application.Terminate;
exit;
end;
//}
Application.CreateForm(TMDIMainForm, MDIMainForm);
Application.Run;
//释放互斥对象
ReleaseMutex(hMutex);
end.
 
多人接受答案了。
 
后退
顶部