Delphi really connects to database learning to COM components

xiaoxiao2021-03-06  16

This example needs to introduce Comobjunit Unit1 in the user;

Interface

Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Comobj, Stdctrls;

TYPE TFORM1 = Class (TFORM) Button1: TButton; Procedure Button1Click (sender: TOBJECT); private {private declarations} end;

Var Form1: TFORM1;

IMPLEMentation

{$ R * .dfm}

procedure TForm1.Button1Click (Sender: TObject); var Rs, Conn: Variant; SqlStr, ConnStr: string; {const adCmdText = $ 00000001; adOpenStatic = $ 00000003; adUseClient = $ 00000003; adLockOptimistic = $ 00000003;} begin SqlStr: = 'Select * From mytest '; ConnStr: =' Provider = SQLOLEDB.1; Password = sa; Persist Security Info = True; User ID = sa; Initial Catalog = MyCAI; ​​Data Source = vSUN '; Rs: = CreateOleObject (' ADODB.RecordSet ') Conn: = CreateoleObject ('adoDb.connection'); conn.open (connStr); rs.activeConnection: = conn; // This sentence is a comparison, if there is no such sentence, then rs is read-only RS.Open (SQLSTR , CONN, 3, 3, 1); // rs.open (SQLSTR, CONN, AdopenStatic, AdlockOptimistic, AdpMdText); RS open another type showMessage (RS.Fields ['Age']. Type); showMessage Rs.fields ['Age']. Value); // rs.addnew; Open, is expressed as a database to add new record rs.fields ['myname']. Value: = 'Abcdef'; rs.fields ['agn ] .Value: = start (formatdatetime ('yyyy-mm-dd', now ())); rs.Update; rs.close;

End. In this example, the components do not have to be connected in Delphi, which is similar to those in the ASP, where many places need to refer to AdoDB manual.

Of course, if you change the value of connStr to other database coupling strings, you can use AdoDB to connect to other databases!

转载请注明原文地址:https://www.9cbs.com/read-41199.html

New Post(0)