æåç°å¾å¤æåå¨å¼åæ°æ®åºæ¶é½ä½¿ç¨ Delphi èªå¸¦ç ADO ç»ä»¶ æ Diamond ADOï¼å
¶å®å¨ Delphi ä¸ä½¿ç¨åç ADO æ¥å£ä¹æ¯ååæ¹ä¾¿åææçãæ使ç¨åç ADO å¼å项ç®å·²æå¾é¿ä¸æ®µæ¶é´ï¼ä¹åçè¿ä¸äºæå类似çé®é¢ï¼ç°å¨æèªå·±çä¸ç¹å¿å¾ä¸å¤§å®¶å享ï¼çé¨å¼æ§ï¼åªæ¯å¸æè½å¯¹å¤§å®¶ææ帮å©ãå½ç¶ï¼è¿å¸åä¹æ¯åççï¼ä¸æ¯è½¬è´´çã
ä¸ãä¼ç¹
1ã大家ç¥é Delphi 对 ADO æ¥å£è¿è¡äºä¸çªå
è£
åå½¢æäº ADOExpressï¼ææ³ Borland ç主è¦ç®çè¿æ¯æ³ä¸èªå·±çæ°æ®æææ§ä»¶ç¸è¿ãç¶èäºå®ä¸æ°æ®æææ§ä»¶å¹¶ä¸æ¯é£ä¹èç¼ï¼å¦æä½ å¸æä½ ç¼åºæ¥çç¨åºç¨å¾®æç¹æ°´åçè¯é£å°±å«ç¨é£ç©æï¼å¦æä½ å¾å°ä½¿ç¨æ°æ®æææ§ä»¶ï¼é£ä¹ ADOExpress åºæ¬ä¸å¤±å»äºå
¶åºæçä½ç¨ï¼æ æ°åä½çå±æ§ãèæ¹æ³ï¼ä¸ç®¡ä½ ç¨ä¸ç¨å¾å°ä¸è¡èç»ä½ ç¼è¯è¿å»ï¼ä¹ä¼ä½¿ä½ çç¨åºåå¤§ä¸ 200Kï¼æçä¹ï¼ä¸è¯´äºã
2ãMSDN å VB ä¸çä¾åä½ å¯ä»¥æ¬è¿æ¥å°±ç¨ã
3ãåè¯é£äº Delphi å对è
ï¼Delphi ä¸æ¯ç¦»å¼ç»ä»¶å°±æ´»ä¸äºã
4ãå
³äºä»£ç éç¨ï¼æç»å¤§å®¶çä¾åé½æ¯ä»¥å½æ°æè¿ç¨å½¢å¼ï¼éç¨æ§ä¸å¥½ä¹ï¼
5ãå«è¯´å¸å太é¿ï¼é£ä½ çç DB.pas, ADODB.pas å¤é¿ï¼
äºãåºæ¬å¨å¤
1ãä¸äºå¿
é¡»çåå
uses
Variants, ComObj;
2ãä¸äºåºæ¬å¸¸æ°ï¼å
¶å®æ¥ ADODB2000.pasï¼ï¼
const
adOpenDynamic = $00000002;
adOpenStatic = $00000003;
adLockOptimistic = $00000003;
adLockBatchOptimistic = $00000004;
adStateClosed = $00000000;
adStateOpen = $00000001;
adStateConnecting = $00000002;
adStateExecuting = $00000004;
adStateFetching = $00000008;
adUseServer = $00000002;
adUseClient = $00000003;
adModeReadWrite = $00000003;
adXactCursorStability = $00001000;
adCmdText = $00000001;
adCmdTable = $00000002;
adCmdStoredProc = $00000004;
adCmdFile = $00000100;
adAffectCurrent = $00000001;
adAffectGroup = $00000002;
adAffectAll = $00000003;
adAffectAllChapters = $00000004;
3ãä¸äºåºæ¬å½æ°åè¿ç¨
//å建 Connection 对象
function CreateConnection: OleVariant;
//éæ¾ Connection 对象ï¼cnn 为 Connection 对象
procedure FreeConnection(var cnn: OleVariant);
//å建 Recordset 对象
function CreateRecordset: OleVariant;
//éæ¾ Recordset 对象ï¼rst 为 Recordset 对象
procedure FreeRecordset(var rst: OleVariant);
//å建 Command 对象
function CreateCommand: OleVariant;
//éæ¾ Command 对象ï¼cmd 为 Command 对象
procedure FreeCommand(var cmd: OleVariant);
//ç¨ Connection è¿æ¥å° SQLServer æ°æ®åºï¼cnn 为 Connection 对象ï¼db æ°æ®åºåï¼host 主æºåï¼usr ç¨æ·åï¼pwd å¯ç
function ConnectToDB(cnn: OleVariant; const db, host, usr, pwd: string): Boolean;
//æ§è¡ SQL è¯å¥ï¼æè¿åè¡ï¼æ äºå¡å¤çï¼cnn 为 Connection 对象ï¼rst 为 Recordset 对象ï¼sql 为 SQL è¯å¥ï¼å¯ä»¥æ¯åå¨è¿ç¨ï¼
function ExecSQL(cnn, rst: OleVariant; const sql: string): Boolean;
//æ§è¡ SQL è¯å¥ï¼æ è¿åè¡ï¼æäºå¡å¤çï¼cnn 为 Connection 对象ï¼cmd 为 Command 对象ï¼sql 为 SQL è¯å¥ï¼å¯ä»¥æ¯åå¨è¿ç¨ï¼
function ExecSQLA(cnn, cmd: OleVariant; const sql: string): Boolean;
function CreateConnection: OleVariant;
begin
try
Result := CreateOleObject('ADODB.Connection');
Result.CursorLocation := adUseServer;
Result.IsolationLevel := adXactCursorStability;
Result.Mode := adModeReadWrite;
Result.Provider := 'SQLOLEDB.1';
except
if not VarIsEmpty(Result) then Result := Unassigned;
end;
end;
procedure FreeConnection(var cnn: OleVariant);
begin
if not VarIsEmpty(cnn) then
begin
if cnn.State <> adStateClosed then cnn.Close;
cnn := Unassigned;
end;
end;
function CreateRecordset: OleVariant;
begin
try
Result := CreateOleObject('ADODB.Recordset');
Result.CacheSize := 1000;
Result.CursorType := adOpenStatic;
Result.CursorLocation := adUseServer;
Result.LockType := adLockOptimistic;
except
if not VarIsEmpty(Result) then Result := Unassigned;
end;
end;
procedure FreeRecordset(var rst: OleVariant);
begin
FreeConnection(rst);
end;
function CreateCommand: OleVariant;
begin
try
Result := CreateOleObject('ADODB.Command');
Result.CommandType := adCmdText;
Result.CommandTimeout := 5;
except
if not VarIsEmpty(Result) then Result := Unassigned;
end;
end;
procedure FreeCommand(var cmd: OleVariant);
begin
if not VarIsEmpty(cmd) then cmd := Unassigned;
end;
function ConnectToDB(cnn: OleVariant; const db, host, usr, pwd: string): Boolean;
begin
Result := not VarIsEmpty(cnn);
if Result then
begin
if cnn.State <> adStateClosed then cnn.Close;
cnn.ConnectionString :=
'Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=' +
db + ';Data Source=' + host + ';Connect Timeout=5;' +
'Use Procedure for Prepare=1';
try
cnn.Open(cnn.ConnectionString, usr, pwd, -1);
except
Result := False;
end;
end;
end;
function ExecSQL(cnn, rst: OleVariant; const sql: string): Boolean;
begin
Result := not (VarIsEmpty(cnn) or VarIsEmpty(rst)) and (cnn.State = adStateOpen);
if Result then
begin
if rst.State <> adStateClosed then rst.Close;
try
rst.Open(sql, cnn, adOpenStatic, adLockOptimistic, adCmdText);
except
Result := False;
end;
end;
end;
function ExecSQLA(cnn, cmd: OleVariant; const sql: string): Boolean;
begin
Result := not (VarIsEmpty(cnn) or VarIsEmpty(cmd)) and (cnn.State = adStateOpen);
if Result then
begin
cnn.BeginTrans;
try
cmd.ActiveConnection := cnn;
cmd.CommandText := sql;
cmd.Prepared := True;
cmd.Execute;
cnn.CommitTrans;
except
cnn.RollbackTrans;
Result := False;
end;
end;
end;
ä¸ã访é®æ°æ®
1ãæå rst.MoveFirst;
2ãæå rst.MoveLast;
3ãåå rst.MovePrevious;
4ãåå rst.MoveNext;
5ãåå½åè®°å½ rst.Fields[0].Value æ rst.Fields['å段å'].Value;
6ãä¿®æ¹å½åè®°å½ rst.Update(rst.Fields[0].Name, æå¼);
7ãåæ¶ä¿®æ¹ rst.CancelUpdate;
8ãå é¤å½åè®°å½ rst.Delete(adAffectCurrent);
9ãå é¤ææè®°å½ rst.Delete(adAffectAll);
10ã追å è®°å½
rst.AddNew;
rst.Fields[0].Value := å¼1;
rst.Fields[1].Value := å¼2;
rst.Update;
11ãå·æ° rst.Refresh;
12ãè®°å½æ° rst.RecordCount
15ãå
¶å®æ¹æ³åå±æ§æ¥ MSDN æ ADO ç帮å©;
åãä¸äºä¾å
//åé声æ
var
cnn, rst, cmd: OleVariant;
//å建对象
procedure TForm1.FormCreate(Sender: TObject);
begin
cnn := CreateConnection;
rst := CreateRecordset;
cmd := CreateCommand;
end;
//éæ¾å¯¹è±¡
procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeCommand(cmd);
FreeRecordset(rst);
FreeConnection(cnn);
end;
//è¿æ¥æ°æ®åº
procedure TForm1.Button1Click(Sender: TObject);
begin
if ConnectToDB(cnn, 'mydb', '127.0.0.1', 'sa', 'ok') then
Caption := 'è¿æ¥æå'
else Caption := 'è¿æ¥å¤±è´¥';
end;
//åè®°å½
procedure TForm1.Button2Click(Sender: TObject);
begin
ExecSQL(cnn, rst, 'select * from 表a');
Caption := VarToStr(rst.Fields['å段a'].Value);
end;
äºãåç ADO ä¸ Delphi ADOExpress ç»ä»¶ç对åºå
³ç³»
1ãConnection <=> ADOConnection.ConnectionObject;
2ãRecordset <=> ADODataSet.Recordset;
3ãCommand <=> ADOCommand.CommandObject;
4ã? <=> ADOQueryï¼å 为 ADOQuery æ ¹æ¬å°±ä¸æ¯åç ADO 对象
5ãExecSQL <=> ADODataSet.Open;
6ãExecSQLA <=> ADOCommand.Execute;
7ãæäºä¸é¢å 个å
¶å®çå°±ä¸å¤è¯´äº
å
ãä¸æ°æ®åºç»ææå
³çä¸äºå½æ°
1ãå¨ææ¹åå段å称
uses ComObj;
//Access
//TableName: 表å; OldColName: åå段å; NewColName: æ°å段å;
procedure RenameField(const TableName, OldColName, NewColName: string);
var
DB, Col: OleVariant;
begin
DB := CreateOleObject('ADOX.Catalog');
DB.ActiveConnection := ADOConnection1.ConnectionObject;
Col := CreateOleObject('ADOX.Column');
Col := DB.Tables[TableName].Columns[OldColName];
Col.Name := NewColName;
end;
//SQLServer
procedure RenameField(const TableName, OldColName, NewColName: string);
begin
with ADOCommand1 do
begin
CommandText := 'EXEC sp_rename ''' + TableName + '.' + OldColName +
''',''' + NewColName + ''',''COLUMN'';';
Excute;
end;
end;
2ãåå¾ Access åºä¸ç表ç»æ
type
TTableDef = record
Name,
DateCreated,
LastUpdated,
Description: string;
end;
TTableDefs = array of TTableDef;
procedure GetTableDefs(const DBName: string; out TableDefs: TTableDefs);
var
DBEngine, DB: OleVariant;
I: Longint;
begin
try
DBEngine := CreateOleObject('DAO.DBEngine.36');
DB := DBEngine.OpenDatabase(DBName);
SetLength(TableDefs, Longint(DB.TableDefs.Count));
for I := Low(TableDefs) to High(TableDefs) do
begin
TableDefs[I].Name := DB.TableDefs[I].Name;
TableDefs[I].DateCreated := DB.TableDefs[I].DateCreated;
TableDefs[I].LastUpdated := DB.TableDefs[I].LastUpdated;
try
TableDefs[I].Description := DB.TableDefs[I].Properties['Description'].Value;
except
TableDefs[I].Description := '';
end;
end;
finally
DB := Unassigned;
DBEngine := Unassigned;
end;
end;
3ãåå¾ Access 表ä¸çå段ç»æ
type
TFieldDef = record
Name: string;
Types,
Size: Longint;
Description: string;
end;
TFieldDefs = array of TFieldDef;
procedure GetFieldDefs(const DBName, TableName: string; out FieldDefs: TFieldDefs);
var
DBEngine, DB: OleVariant;
I: Longint;
begin
try
DBEngine := CreateOleObject('DAO.DBEngine.36');
DB := DBEngine.OpenDatabase(DBName);
SetLength(FieldDefs, Longint(DB.TableDefs[TableName].Fields.Count));
for I := Low(FieldDefs) to High(FieldDefs) do
begin
FieldDefs[I].Name := DB.TableDefs[TableName].Fields[I].Name;
FieldDefs[I].Types := DB.TableDefs[TableName].Fields[I].Type;
FieldDefs[I].Size := DB.TableDefs[TableName].Fields[I].Size;
try
FieldDefs[I].Description := DB.TableDefs[TableName].Fields[I].Properties['Description'].Value;
except
FieldDefs[I].Description := '';
end;
end;
finally
DB := Unassigned;
DBEngine := Unassigned;
end;
end;
4ãè³äºå¦ä½å¨æå建 Access æ°æ®åºä¹ç±»æå°±ä¸ç½å¦äºï¼å°å¤é½æç¸å
³çæç»è´´
ä¸ãå
¶å®
1ãæä½¿ç¨ ADO çç»åï¼ADOExpress-->ADOExpressçåçæ¥å£-->å¼ç¨ADO2.1æ¥å£åå
ï¼å³ADODB_TLB-->ç´æ¥ä½¿ç¨ ADO ç COM æ¥å£ï¼
2ãå¸ææ¤è´´è½å¯¹æåé¨ææ帮å©ï¼æåçè¿äºé½æ¯ç»è¿å®ææ£éªçï¼
3ãè§å¾æç¨çæåå¯ä»¥è¯ä¸ä¸ï¼å¦æè§å¾æ²¡ç¨å°±å½æ没åï¼è½è¯¯ä½ æ¶é´äºã
温馨提示:内容为网友见解,仅供参考