no e
uses ADODB, ComObj;
var
function SQLOpenOK(ADO:TADOQuery; sSQL:string): boolean;
function SQLExecuteOK(ADO:TADOQuery; sSQL:string): boolean;
{-------------------------------------------}
//執行SQL命令,成功=true ,失敗=false
function SQLOpenOK(ADO:TADOQuery; sSQL:string): boolean;
function QuotedMsg(sMsg:string): string;
begin
sMsg:=Trim(sMsg);
if sMsg='' then Result:=''
else Result:=' ['+sMsg+']';
end;
begin
Result:=False;
try
ADO.Close;
ADO.SQL.Clear;
ADO.SQL.Add(sSQL);
ADO.Open;
except
On E:EOleException Do
begin
ShowMessage(E.Message);
if E.ErrorCode=E_FAIL then //連線失敗
begin
ShowMessage('與DB連線失敗');
Application.Terminate;
end;
exit;
end;
end;
Result:=True;
end;
{-------------------------------------------}
function SQLExecuteOK(ADO:TADOQuery; sSQL:string): boolean;
function QuotedMsg(sMsg:string): string;
begin
sMsg:=Trim(sMsg);
if sMsg='' then Result:=''
else Result:=' ['+sMsg+']';
end;
begin
Result:=False;
try
ADO.Close;
ADO.SQL.Clear;
ADO.SQL.Add(sSQL);
ADO.ExecSQL;
except
On E:EOleException Do
begin
ShowMessage(E.Message);
if E.ErrorCode=E_FAIL then //連線失敗
begin
ShowMessage('與DB連線失敗');
Application.Terminate;
end;
exit;
end;
end;
Result:=True;
end;
如何使用呢?
ADOConnection1的屬性Connectted=True及LoginPrompt=Flase。
try
ADOConnection1.Connected := True;
except
Showmessage('連線失敗!');
end;
if not SQLOpenOK(ADOQuery1,'SELECT * FROM 資料表名') then
ShowMessage('Query ERROR!!');
s := 'create table 富達新興 (';
s := s + '流水號 AUTOINCREMENT NOT NULL';
s := s + ', [日期] Date';
s := s + ', 淨值 int';
s := s + ', PRIMARY KEY (流水號)';
s := s + ');';
if not SQLExecuteOK(ADOQuery1,s) then
ShowMessage('Query ERROR!!');
insert into 資料表(column1, column2, column3..) values(v1, v2, v3...);
記得是整數時前後不用加''',若是字串時則要加'''',若是時間時則不用加''',但要加個#。
沒有留言:
張貼留言