程序中要使用数据库,首先是引用SynCommons, SynDB单元,根据不同的数据库类型,简单举几个例子:1 使用Access数据库,引用SynCommons, SynDB,SynOleDb三个单元,如果密码为空,设置后面三个参数直接为空就可以了.mORMot数据库处理方式Code (Text):var gProps: TSQLDBConnectionProperties; rows: ISQLDBRows;begin gProps := TOleDBJetConnectionProperties.Create('test.mdb', '库名', '用户名', '密码'); rows := gProps.ExecuteInlined('select * from dm_sys', True); if rows<>nil then while rows.Step() do showmessage(rows.ColumnString('value'));end;普通记录集处理方式,增加单元引用 SynDBMidasVCL Code (Text):var gProps: TSQLDBConnectionProperties; ds: TSynDBDataSet;begin gProps := TOleDBJetConnectionProperties.Create('test.mdb', '库名', '用户名', '密码'); ds := TSynDBDataSet.Create(nil); try ds.Connection := gProps; ds.CommandText := 'select * from dm_sys'; ds.Open; ds.First; while not ds.Eof do ShowMessage(ds.FieldByName('value').AsString); ds.Close; finally FreeAndNil(ds); end;end;连接不同类型数据库,仅列出引用单元和初始化代码,具体使用基本同上面的代码。2 使用Sqlite3数据库,引用SynCommons, SynDB,SynDBSQLite3, SynSQLite3, SynSQLite3Static连接为gProps := TSQLDBSQLite3ConnectionProperties.Create('test.db3', '', '', '用户密码');//Sqlite3的密码只设置最后一个参数3 使用ZEOS可以连接不同数据库,引用SynCommons, SynDB,SynDBZeos A) 连接FireBird: gProps := TSQLDBZEOSConnectionProperties.Create('zdbc:firebird-2.0://127.0.0.1:3050/model?username=sysdba;'+'password=masterkey;LibLocation=fbclient.dll', '', '', ''); B) 连接MySql:gProps:= TSQLDBZEOSConnectionProperties.Create('zdbc:mysql://127.0.0.1:3306/model?username=sysdba;'+'password=masterkey;LibLocation=libmysql.dll', '', '', ''); C) 连接MySql Em:gProps:= TSQLDBZEOSConnectionProperties.Create('zdbc:mysqld-5:///ahoa?compress=yes;dbless=no;useresult=no;timeout=30;'+'ServerArgument1=--basedir=./;'+'ServerArgument2=--datadir=./data;'+'ServerArgument3=--character-sets-dir=./share/charsets;'+'ServerArgument4=--language=./share/english;'+'ServerArgument5=--key_buffer_size=64M;'+'codepage=utf8;AutoEncodeStrings=ON;controls_cp=CP_UTF16;LibLocation=.\lib\libmysqld.dll', '', '', '');//MySql增强模式可以在程序启动时启动MySql数据库,无单独的MySql进程。 D) 连接Oracle: gProps:= TSQLDBZEOSConnectionProperties.Create('zdbc:Oracle:///orcl?username=sysdba;password=masterkey;'+'LibLocation=oci64\oci.dll', '', '', '');使用ISQLDBRows是速度最快的读取SQL 数据库的方法,如果我们写Web后台程序,这是一个极好的方法。E) 连接MS SQL Server数据库:type TForm1 = class(TForm)mmoLog: TMemo;btnOpen: TButton; procedure btnOpenClick(Sender: TObject); private public end;Var Form1: TForm1;implementationuses SynDB, SynOleDB, SynCommons;{$R *.dfm}procedure TForm1.btnOpenClick(Sender: TObject);var dbConn: TOleDBConnectionProperties; rows: ISQLDBRows; cServer,cDatabase,cUserId,cUserPwd: RawUTF8;begin cServer:='127.0.0.1'; cDatabase:='SM1'; cUserId:='sa'; cUserPwd:='sa'; dbConn := TOleDBMSSQLConnectionProperties.Create(cServer,cDatabase,cUserId,cUserPwd);{Change TOleDBMSSQLConnectionProperties.SetInternalProperties set fProviderName:='SQLOLEDB' if you don't have SQLNCLI10 installed; dbConn.ConnectionStringDialogExecute;dbConn.ConnectionString:='Provider=SQLOLEDB;Data Source=127.0.0.1;Initial Catalog=SM1;User Id=xx;Password=xxxx;';} mmoLog.Lines.Add(dbConn.ConnectionString); rows := dbConn.ExecuteInlined(ToUTF8('select * from 部组信息'), True); if rows<>nil then while rows.Step() do mmoLog.Lines.add(rows.ColumnString(ToUTF8('名称'))); dbConn.Free;end;end.需要说明的是1.18将TOleDBMSSQLConnectionProperties的ProviderName由SQLOLEDB改成SQLNCLI10,要正常运行,需要安装MSSQL Native Client 10的驱动,如果想让你的程序在任何机器上将能跑,打开SynOleDB.pas将TOleDBMSSQLConnectionProperties. SetInternalProperties代码片段改为fProviderName:='SQLOLEDB'即可。SQLNCLI10与SQLOLEDB都支持SQL Server 2000. SQLOLEDB任何Windows都带,SQLNCLI10以及更高版本的SQLNCLI11需要安装驱动,驱动又分32位与64位。上述代码的toUTF函数是为在Delphi7下编译准备的,如果是2009以上版本可以让编译器自动转换。如果要做Json序列化为utf8,可使用ColumnUTF8。ZeOSDBO 连接上述数据库,用FreeTDS_MsSQL>=2005,连接出现character sets错误解决:详细过程(WIN10X64环境,测试X32&X64APP)将附件里的freetds-0.95.87\32 bits + iconv\sybdb.dll复制到C:\Windows\SysWOW64->适用X32APP;将附件里的freetds-0.95.87\64 bits\sybdb.dll复制到C:\Windows\System32->适用X64APP。留意最主要的设置 :object ZConnection1: TZConnection ControlsCodePage = cCP_UTF16 ClientCodepage = 'UTF8' Catalog = '' Properties.Strings = ( 'codepage=UTF8' 'AutoEncodeStrings=ON' 'controls_cp=CP_UTF16') Connected = True HostName = '127.0.0.1' Port = 0 Database = 'ABC' User = 'sa' Password = '******' Protocol = 'FreeTDS_MsSQL>=2005' Left = 56 Top = 72 end