Delphi ADOQuery

Delphi ADOQuery

Procedure TForm1.Button1Click (Sender: TObject); 
 var  
A: the Array of String; // definition of dynamic array 
Index: Integer; // define an array index variable 
ADOQuery: TADOQuery; 
 the begin  
ADOQuery: = TADOQuery.create; 
ADOQuery.SQL.Clear ; 
ADOQuery.SQL.Add ( ' the Select B from A ' ); 
ADOQuery.Open; 
setLength (A, ADOQuery.RecordCount); // set the array width 
Index: = 0 ; // initialize subscript 
ADOQuery.First; 
the While Not ADOQuery the Do .EOF // dataset cycle 
the begin  
A [Index]:ADOQuery.FieldByName = ( ' B ' ) .asString; // add data to the array Inc is an (Index); 
ADOQuery.Next; 
 End ; 
 End ;
View Code

Delphi ADOQuery join query the database, insert, delete, modify,

// query record 
Procedure TForm1.Button1Click (Sender: TObject);
 the begin 
ADOQuery.Close; 
ADOQuery.SQL.Clear; 
ADOQuery.SQL.Add ( ' SELECT * WHERE query from yourtable ' ); 
ADOQuery.Open; 

// insert records 
Procedure TForm1.Button2Click (Sender: TObject);
 the begin 
ADOQuery.Close; 
ADOQuery.SQL.Clear; 
ADOQuery.SQL.Text: = " INSERT INTO yourtable (field 1, field 2) values (: 1 ,: field 2 field) ' ;
 // ADOQuery.SQL.Add ( "INSERT INTO yourtable values (: field 1) '); 
ADOQuery.Parameters.ParamByName ( ' field 1 ') .Value: = TRIM (Edit1.Text); 
ADOQuery.Parameters.ParamByName ( ' Field 2 ' ) .Value: = TRIM (Edit2.Text); 
ADOQuery.ExecSQL; 
End ;
 // delete the record 
Procedure TForm1.Button3Click (Sender : TObject);
 the begin 
ADOQuery.Close; 
ADOQuery.SQL.Clear; 
ADOQuery.SQL.Text: = " the Delete from fields where yourtable = 3: field 3 ' ;
 // condition judging where there is not added, when the actual use, attention Analyzing added 
// ADOQuery.SQL.Add ( 'the Delete from NEW_TABLE WHERE field = 3: field 3'); 
ADOQuery.Parameters.ParamByName ( ' field 3 ' ) .Value: =TRIM (Edit3.Text); 
ADOQuery.ExecSQL; 
// delete the record can also be used DeleteRecords () function 
Procedure DeleteRecords (AffectRecords: TAffectRecords = ARALL);    
This function has one parameter: AffectRecords can take the following values:    
. 1 , arCurrent: delete the current record   
 2 , arFiltered: delete all records in line (if you use filter filtering if) the filter filter   
 3 , ARALL: delete all records   
 4 , arAllChapters: the delete Affects. All Chapters (Chapters ADO)
 // modify records 
Procedure TForm1.Button4Click (Sender : TObject);
 the begin 
ADOQuery.Close; 
ADOQuery.SQL.Clear; 
ADOQuery.SQL.Text: = " the Update yourtable the SET field = 4: field 4 ' ;
// not added here where the determination condition, the actual use, note that adding determines 
// ADOQuery.SQL.Add ( 'field of the Update yourtable the SET = 4: Field 4'); 
ADOQuery.Parameters.ParamByName ( ' field 4 ' ). the Value: = TRIM (Edit4.Text); 
ADOQuery.ExecSQL; 
// immediate updating insert, delete, modify the record 
is inserted in the above, delete, add the following code to the modified statement: 
ADOQuery.Close; 
ADOQuery.SQL. the Add ( ' the SELECT * query from yourtable the WHERE condition ' ); 
ADOQuery.Open; 
// Note the use of ADOQuery: 

1 , if you need to change the data, query.requestlive must be to true
 2 , if there is a time input parameters, error-prone, a general error method is: 
For example: "the WHERE ABC = : ABC" 
corrected as follows: "the WHERE ABC =: ABC" That =: Before and after can not be spaces.
3 , ADOQuery.Open and ADOQuery.ExecSQL there are differences. 
ADOQuery.Open generally used in the query, select the time; and ADOQuery.ExecSQL used in INSERT, the Delete , Update and so on.
View Code

Delphi using ADO in connection with a password Access

Delphi using ADO in connection with a password Access 

 

var gDBConn: String ; 


the try 
self.ADOConnection1.Close; 
// no password = Connection Password ""; 
// gDBConn: = 'Provider = the Microsoft.Jet.OLEDB.4.0; Password = ""; the Data Source = MyDB.mdb; of the Persist Security Info = True '; 
// password OLEDB connection the Jet: Database password = delphi7; 
gDBConn: = ' Provider = Microsoft.Jet.OLEDB.4.0; the Jet OLEDB: Database password 123456 =; the Source the Data = ' + ExtractFilePath (application.ExeName) + ' MyDB.mdb; the Persist Security Info = True ' ; 
self.ADOConnection1.ConnectionString: = gDBConn; 
self.ADOConnection1.Open; 
the except
// The raise; 
self.ADOConnection1.Close; 
self.ADOConnection1.Free; 
application.MessageBox ( ' can not connect to the database! ' , ' Message ' , 64 ); 
Application.Terminate; 
End ;
View Code

delphi get all the table names, field names database table

var    
      TS: Tstrings;    
      I: Integer;    
  the begin    
      TS: = TStringList. the Create ;   
       // adoconnection1.GetTableNames (TS, to false);    
      // for I: = 0 to ts.Count-do. 1    
      //    ShowMessage ( 'first' + inttostr (i + 1) + 'of the table name is' + ts.Strings);    
      adoconnection1.GetFieldNames ( ' STU ' , TS);   
       for    I: = 0    to    ts.Count- . 1    do    
              ShowMessage ( ' first ' + IntToStr ( + I . 1 ) + ' field name is ' +ts.Strings);   
   End ;   
     // Note: The above functions can also be done with a session controls
View Code

 

Guess you like

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