Delphi operation ACCESS skill sets

In 1.DELPHI operation access database (.mdb build files, compressed database)
the following code in WIN2K, D6, MDAC2.6 test passes,
the compiled program to run successfully under WIN98 second version without ACCESS environment.
Before // ComObj uses, the ActiveX
// declare the connection string
Const
sConnectionString = '= the Microsoft.Jet.OLEDB.4.0 Provider; the Data the Source =% S;'
+ 'the 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(PChar(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;

//=============================================================================
// Procedure: CompactDatabase
// Author : ysai
// Date : 2003-01-27
// Arguments: AFileName,APassWord:string
// Result : boolean
//=============================================================================
function CompactDatabase(AFileName,APassWord:string):boolean;
//压缩与修复数据库,覆盖源文件
var
STempFileName:string;
vJE:OleVariant;
begin
STempFileName:=GetTempPathFileName;
try
vJE:=CreateOleObject('JRO.JetEngine');
vJE.CompactDatabase(format(SConnectionString,[AFileName,APassWord]),
format(SConnectionString,[STempFileName,APassWord]));
result:=CopyFile(PChar(STempFileName),PChar(AFileName),false);
DeleteFile(STempFileName);
except
result:=false;
end;
end;

//=============================================================================
// Procedure: ChangeDatabasePassword
// Author : ysai
// Date : 2003-01-27
// Arguments: AFileName,AOldPassWord,ANewPassWord:string
// Result : boolean
//=============================================================================
function ChangeDatabasePassword(AFileName,AOldPassWord,ANewPassWord:string):boolean;
//修改ACCESS数据库密码
var
STempFileName:string;
vJE:OleVariant;
begin
STempFileName:=GetTempPathFileName;
try
vJE:=CreateOleObject('JRO.JetEngine');
vJE.CompactDatabase(format(SConnectionString,[AFileName,AOldPassWord]),
format(SConnectionString,[STempFileName,ANewPassWord]));
result:=CopyFile(PChar(STempFileName),PChar(AFileName),false);
DeleteFile(STempFileName);
except
result:=false;
end;
end;

2.ACCESS used in SQL statements should pay attention to local and Skills to
the following SQL statements ACCESS XP query test by
built table:
the Create the Table Tab1 (
ID Counter,
the Name String,
Age Integer,
[a Date] DateTime);
tips:
auto-increment field declarations with Counter.
field called keywords in square brackets [] enclose, as the digital field names are also possible.

Indexing:
The following statement based on Date column Tab1 reusable index
Create Index iDate ON Tab1 ([Date ]);
After completion of the ACCESS field Date index properties displayed - at (duplicate).
The following statement Tab1 of establishing unrepeatable index on the Name column
Create Unique index iName oN Tab1 (Name );
after completion of the ACCESS attribute is displayed as the field Name index - has (without repeat).
the following statement removes just created two indexes
Drop index iDate oN Tab1;
Drop Index iName ON Tab1;

ACCESS and SQLSERVER UPDATE statement in the comparison:
SQLSERVER updated multiple-table UPDATE statement:
UPDATE Tab1
the SET a.name = b.name
the FROM Tab1 A, B Tab2
the WHERE a.id = b.ID;
same function SQL statements ACCESS should be
UPDATE Tab1 a, Tab2 b
the SET a.name = b.name
the WHERE a.id = b.ID;
namely: ACCESS UPDATE statement is not in the FROM clause, all references are listed in the table after the UPDATE keyword.
If the above example may not be a table Tab2, but a query, for example:
the UPDATE Tab1 a, (the Select ID, the From the Name Tab2) B
the SET a.name = b.name
the WHERE a.id = b.ID;

Accessing a plurality of different databases ACCESS - In use in SQL clauses:
the Select A *, B * Tab1 the From A, B Tab2 In 'Db2.mdb' = the Where a.id b.ID;..
The above SQL queries the current database Tab1 and db2.mdb all records (current folder) in Tab2 associated with the ID.
Cons - not external database with a password.
Added: see ugvanxk in a reply, you can use
Select * from [c: \ AA \ a.mdb; pwd = 1111] .table1;
ACCESS XP test passed

Access to other data sources in the ACCESS ODBC
embodiment the data query in the ACCESS SQLSERVER in
the SELECT * the FROM Tab1 the IN [ODBC]
[ODBC; Driver = the SQL Server; the UID = SA; the PWD =; Server = 127.0.0.1; the DataBase = Demo ;]
full parameter external data source attributes are:
[the ODBC; DRIVER = driver; SERVER = Server; DATABASE = Database; the UID = User; password = the PWD;]
which may DRIVER = driver registry
HKEY_LOCAL_MACHINE \ SOFTWARE \ ODBC \ ODBCINST.INI \
find the
guide data between heterogeneous databases see Sword of
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1691966

ACCESS supports subqueries

ACCESS supports external connection, but not a complete external coupling, such as support
LEFT JOIN or RIGHT JOIN
but not
FULL OUTER JOIN or FULL JOIN

ACCESS date of inquiry
note: ACCESS date and time in the separator is # rather than quotation marks
Select * From Tab1 Where [Date] > # 2002-1-1 #;
in the DELPHI me with
SQL.Add (the Format (
'the Select the Where the From Tab1 * [a Date]> # S #%; ',
[DateToStr you (a Date)]));

ACCESS string may be separated by double quotes, but does not recognize SQLSERVER, so for convenience and compatibility migration
is recommended as a single quote string delimiter.

 

delphi ACCESS read a table name in the database

 ADOConnection1.Close;
ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='
+ Edit1.Text + ';Persist Security Info=False';
ADOConnection1.Open;
ADOConnection1.GetTableNames(ComboBox1.Items);
ComboBox1.ItemIndex := 0; 
View Code

delphi connection and access ways

A connection mode .SQL database: 

Conditions: control name: AdoConnecion1, database name: the Sample, username: password SA: 123 , the database URL: native 

AdoConnection1.ConnectionString: = ' Provider = SQLOLEDB.1; Password = 123; the Persist Info = True Security; the User ID = SA; the Initial Cataog the Sample =; = Data Source. ' ; 
Adoconnection1.LoginPrompt: = False; 
Adoconnection1.Connected: = True; 

above code is coupled manner SQL database: the last one Data Source = , where the "." is a database in the machine, if the data in the other places, it will point to the IP address or server name. 

2 coupled manner .ACCESS database: 

Condition: ADOConnection1, database name: Sample code: 123 , the database program accessing the file path is the root directory. 

Procedure TDataModule1.DataModuleCreate (Sender: TObject);
var
   DbPath:String;
begin
DbPath:=ExtractFilePath(Application.ExeName)+'Sample.mdb';
Adoconnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=123;Data Source='+DbPath+';Persist Security Info=false';
Adoconnection1.LoginPrompt:=False;
Adoconnection1.Connected:=True; 
end;
View Code

Midas use to access the remote database access

Remote database access common approach is to use a large database that comes with the client tools, such as SQL Server and Oracle, and so with this tool. For such a small database with Access, this method can not be used, because Access does not provide such client tools. But we can achieve remote access Access database through MIDAS. 
MIDAS is a three-tier solution proposed by Delphi. The basic idea I provide access to a remote Access database methods used in MIDAS is built for remote access on the server side data module, and the client by MIDAS components to connect to it, and send SQL statements to the server on the client, the server performs return data sets to the client. 
Specific practices: 
Server-side 

 
server using ADO control to access the Access database, specifically ADOConnection control connection is established, ADOQuery perform related operations. Use the Data Access Controls ClientDataSet returned to the client dataset. And the above control on Remote Data Module vessel, for connection by the client. 
The following steps: 
1 . Establishment of the Application. Which is named Form FormServer, Project called PrjServer;
 2 . FileàNewàOther ..., New Items in the pop-up dialog box, select Multitier page, selecting the page in the Remote Data Module, is determined;
 3In the pop-up Remote Data Module Wizard dialog box, filled in AccessServer coclass column, holding the Instancing and Threading Model default, i.e., between the threads independent of each other represents, after determining to generate a derived class AccessServer RemoteDataModule (at ViewàType Library that can view the detailed information of the class);
 4 . a new file in the file folder named data as the program, the name under which placed a my.mdb Access database file with a name person table, the table has only two fields, respectively, and name Age, all texts;
 5 put various controls on the AccessServer remote data module, the final results as shown below: 
server remote data module 
of each control related attributes the following table shows the effect of: 
the type of control 
relevant to the page 
attribute 
values 
action 
ADOConnection 
the ADO 
name 
ADOConnection1 
connection Access file 
the ConnectionString 
Provider = Microsoft.Jet.OLEDB.4.0. 4.0 ; the Source the Data = \ Data \ my.mdb; = the Persist Security Info. False
ADOQuery
ADO 
name 
ADOQuery1 
implementation of the relevant query 
Connection 
ADOConnection1 
ClientDataSet 
the Data Access 
name 
DataSetProvider1 
provide data interface to the client 
the DataSet 
ADOQuery1 
Options. PoAllowCommandText 
to true 
 
Stardard6 . Compile and run the server program. Run the program that is registered with a MIDAS server, and only under the condition of a server program running to the next step of the client program development. 
The client 
 
in a way DCOM server-side connection, 
1 Establishment Application. Which is named Form FormClient, Project called PrjClient;
 2 placed on the respective control Form, the final results as shown below: 
The client interface 

 
associated with the role of each control properties shown in the following table: 
Control Type 
page belongs 
attribute 
values 
action 
Edit 
txtQuery
name 
received query 
the Text 
SELECT * from Person 
the Button 
Stardard 
name 
btnQuery 
 
 
DCOMConnection 
the DataSnap 
name 
DCOMConnection1 
through DCOM server connected 
ServerName 
PrjServer.AccessServer 
the ClientDataSet 
the Data Access 
name 
ClientDataSet1 
issue SQL statements to the distal end, and obtain the data set 
the RemoteServer 
DCOMConnection1 
the ProviderName 
DataSetProvider1 
the DataSource 
the Data Access 
name 
DataSource1 
as a data source controls present data in 
the DataSet 
ClientDataSet1 
DBGrid 
the data controls 
name 
DBGrid1
Display the retrieved data set from the distal end 
the DataSource 
to DataSource1 
Description: 
 
ServerName l DCOMConnection1 attribute is selected from the list, the list is a list of all registered servers MIDAS 
l ClientDataSet1 the ProviderName property is specified on a control server 
3 in. Form Double-click on the button btnQuery, was added in its Click handler piece of code as follows: 
  self.ClientDataSet1.Close; 
  self.ClientDataSet1.CommandText: = self.txtQuery.Text; 
  self.ClientDataSet1.Open; 
Note: 
If no is given returns the SQL statement, the last sentence was changed by the Open Execute. 
After the operation, results as shown below: 
The client running effect 
change the connection mode of the client 
 
in the above example are connected to a DCOM server mode, which typically only be used within a LAN. If you want to use in a wide area network, you can use Socket connected server. This is subject to make the following changes: 
1 . On the server side running Delphi comes scktsrvr.exe program, the program Borland \ Delphi7 \ Bin \ folder
 2Alternatively DCOMConnection control, change of the properties shown in the following table with the control on the client SocketConnection: 
control type 
relevant to the page 
attribute 
values 
action 
SocketConnection 
the DataSnap 
name 
SocketConnection1 
through the Socket server connect 
the Address 
127.0 . 0.1 (native) 
ServerName 
PrjServer.AccessServer 
the ClientDataSet 
Access the data 
RemoteServer 
SocketConnection1 
sent to the remote SQL statement and obtain data sets 
Note: 
 
the default port SocketConnection is 211, which is the default port of scktsrvr. This should open the ports on the server firewall
 
View Code

 

Guess you like

Origin www.cnblogs.com/blogpro/p/11345816.html