TUniConnection连接

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TUniConnection连接相关的知识,希望对你有一定的参考价值。

ChangeFileExt

RealTime_Server.ini

[Config]
FromConStr=DBTYPE=0|SERVER=127.0.0.1|PORT=7788|DBNAME=jzxtld|USERNAME=sa|PASSWORD=admin
ToConStr=DBTYPE=0|SERVER=127.0.0.1|PORT=7788|DBNAME=jzxxsjs|USERNAME=sa|PASSWORD=admin

 
 1 procedure StrToUniCon(AStr: string; UniCon: TUniConnection);
 2   function GetDBType(i: Integer): string;
 3   begin
 4     case i of
 5       0:
 6         begin
 7           Result := SQL Server;
 8         end;
 9       1:
10         begin
11           Result := Oracle;
12         end else
13       begin
14         Result := ‘‘;
15       end;
16     end;
17   end;
18 var
19   L: TStrings;
20 begin
21   if Trim(AStr) = ‘‘ then Exit;
22   if (AStr <> ‘‘) and (AStr[Length(AStr)] = |) then system.Delete(AStr, Length(AStr), 1);
23   L := TStringList.Create;
24   try
25     L.Delimiter := |;
26     L.DelimitedText := AStr;
27     if L.Count = 6 then
28     begin
29       UniCon.LoginPrompt := False;
30       UniCon.ProviderName := GetDBType(StrToInt(L.Values[DBTYPE]));
31       if SameText(UniCon.ProviderName, Oracle) then
32       begin
33         UniCon.SpecificOptions.Clear;
34         UniCon.SpecificOptions.Add(Oracle.Direct=True);
35         UniCon.SpecificOptions.Add(Oracle.PrecisionInteger=11);
36         UniCon.server := L.Values[SERVER] + : + L.Values[PORT] + : + L.Values[DBNAME];
37       end else
38       begin
39         UniCon.server := L.Values[SERVER] + , + L.Values[PORT];
40         UniCon.Database := L.Values[DBNAME];
41       end;
42       UniCon.Username := L.Values[USERNAME];
43       UniCon.Password := L.Values[PASSWORD];
44     end;
45   finally
46     L.Free;
47   end;
48 end;
 
1
procedure TfmDBConectString.bt_testconnClick(Sender: TObject); 2 var 3 Con: TUniConnection; 4 begin 5 if rgDbType.ItemIndex = -1 then 6 begin 7 Application.MessageBox(请选择数据库类型!, 警告, MB_ICONWARNING or MB_APPLMODAL); 8 Exit; 9 end; 10 Con := TUniConnection.Create(nil); 11 try 12 try 13 StrToUniCon(Self.GetConnectXML, Con); 14 Con.Connect; 15 ShowMessage(连接成功!); 16 Con.Disconnect; 17 except 18 on E: Exception do 19 begin 20 ShowMessage(E.Message); 21 end; 22 end; 23 finally 24 Con.Free; 25 end; 26 end;

 

 1 procedure TSyncThread.LoadFromCon(Uni: TUniConnection);
 2 var
 3   S:String;
 4 begin
 5   with TIniFile.Create(ChangeFileExt(GetModuleName(0),.ini)) do
 6   begin
 7     S:=ReadString(Config,FromConStr,‘‘);
 8     if S<>‘‘ then
 9     begin
10       StrToUniCon(S,Uni);
11     end;
12     Free;
13   end;
14 end;

 

 1  1 function TfmMain.GetToConStr: string;
 2  2 begin
 3  3   with TIniFile.Create(ChangeFileExt(GetModuleName(0), .ini)) do
 4  4   begin
 5  5     Result := ReadString(Config, ToConStr, ‘‘);
 6  6     Free;
 7  7   end;
 8  8 end;
 9  9 
10 10 procedure TfmMain.SetToConStr(const Value: string);
11 11 begin
12 12   with TIniFile.Create(ChangeFileExt(GetModuleName(0), .ini)) do
13 13   begin
14 14     WriteString(Config, ToConStr, Value);
15 15     Free;
16 16   end;
17 17 end;

 



以上是关于TUniConnection连接的主要内容,如果未能解决你的问题,请参考以下文章

这些角度电子邮件指令代码片段如何连接

多线程下使用使用 UniDAC+MSSQL 需要注意的问题(使用CoInitialize)

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

错误:E/RecyclerView:未连接适配器;跳过片段上的布局

连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段

部分代码片段