2011/12/19

DBGrid介紹

Adding components to a DBGrid


內容相當豐富,而且篇篇精彩!錯過可惜!

2011/12/15

影響My.User.IsAuthentication恆為特定值的設定

資料來源:为什么Context.User.Identity.IsAuthenticated总为false + web.config authentication

ASP.NET 提供了一些新型的Authentication 和 Authorization 方案,对于开发人员来说这将是和以前不同的一种方式。 可喜的是ASP.NET提供的方案多样更加灵活;可惜的是这种方案是基于IIS的。我知道,有些人并不喜欢IIS,认为它容易受攻击。只是一直不喜欢 ASP的方式,杂乱的HTML标记中夹杂着代码,难于维护也难于调试。现在有了新的解决方案,让我们看看。  

C#.Net调用VB.Net中的MY

文章來源:C#.Net調用VB.Net中的MY
用過VB.NET的人應該只要MY有多強大了吧,是不是很想在C#中也能調用呢?
當然是可以的,.net作為微軟的跨語言的平台,必須是能實現的,不然微軟就自己打自己嘴巴了~
回到正題上:
1.在程序中加上Microsoft.VisualBasic引用;
2.添加如下命名空間
using Microsoft.VisualBasic.ApplicationServices;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.VisualBasic.Devices;
using Microsoft.VisualBasic.FileIO;
using Microsoft.VisualBasic.Logging;
using Microsoft.VisualBasic.MyServices;
其中對應關係為下圖所示:
 項目VB.NET  C#引用命名空間備註 
 1 MY.Application Microsoft.VisualBasic.ApplicationServices; 應用程序相關
 2 My.Computer Microsoft.VisualBasic.Devices.Computer 計算機相關
 3 My.Forms Form窗體實體化 windows Form相關
 4 MY.Log Microsoft.VisualBasic.Logging 記錄文件相關
 5 MY.Resources Namespace命名空間.Properties.Resources 資源文件相關
 6 MY.User Microsoft.VisualBasic.ApplicationServices.User 用戶相關
 7 MY.Settings Namespace命名空間.Properties.Settings 設置文件相關
 8 My.WebServices Web Service實體化 Web Service相關

2011/12/09

Delphi UniGUI vs Intraweb 比較


以下的資料取自:Tema: Comparativa uniGUI vs. Intraweb (Leído 1009 veces)


- UniGUI is 100% AJAX enabled.
 In Intraweb AJAX support is partially implemented.


- Intraweb can be used to develope both web sites and web applications.
 UniGUI is best for Web Applications.


- You can use custom HTML templates in Intraweb
  UniGUI doesn't emit much HTML and HTML templates aren't available


- In UniGUI you can use same code base to create a VCL desktop app and a web RIA app.
 Intraweb can not create VCL desktop apps.


- UniGUI uses Ext JS library to render web pages
- Intraweb emits its own HTML and JS code.


- In UniGUI same combo project can be used to deploy ISAPI or Standalone apps
  In Intraweb you need to have separate projects


- Intraweb can use customized CSS to change visual appearance of controls
 uniGUI relies on CSS classes in current theme for visual appearance


- In uniGUI you can dynamically create and destroy controls in AJAX calls
In Intraweb you need to refresh page to dynamically create or destroy controls.


- UniGUI is beta
 Intraweb released years ago


針對上面的資料做了以下的表格:

有時間我會再進行與 Raudus.com 的比較

2011/12/01

MyBase自訂技巧 -- 取代StringGrid

底下的方法相當簡單明瞭,在許多場合都可以派上用場,而且另存資料也相當方便!



procedure TForm1.AddNewDataClick(Sender: TObject);
begin
  with ClientDataSet1 do
  begin
    Append;
    Fields.Fields[0].AsString := Edit_AddTest0.Text;
    Fields.Fields[1].AsString := Edit_AddTest1.Text;
    Fields.Fields[2].AsString := Edit_AddTest2.Text;
    Post;
  end;
end;

2011/11/28

[轉]学习英语词汇的有用方法和技巧


學習英語詞彙的有用方法和技巧

    這篇文章不是針對英語六級或雅思詞彙的,而是針對所有的英語詞彙學習,因為,其實在英語詞彙學習的過程中,學習方法和技巧是一致的。雖然我們相當討厭把英語這門語言當考試工具,但既然大家都還得參加各式各樣的英語考試
,那只好也說一些讓我們在英語考試中能夠游刃有餘的學習方法和技巧。

    十種學習英語詞彙的有用方法和技巧:

  1.多做題,搞懂每一題,不要有生詞。
不確定為什麼答案是B就查字典。推薦朗文及牛津辭典。最好用英英,次之中英雙解,最不推薦用中英辭典,這樣永遠學不好。查單詞時,一定要從從頭看到尾,把各種解釋、用法、例句都快快看過一次。查字典找句你覺得不錯的例句多看,自己最好也造一句寫下來。

台灣有兩百萬戶家庭陷入新貧恐慌,你屬於哪一個階級?

673期今周刊--台灣有兩百萬戶家庭陷入新貧恐慌,你屬於哪一個階級?

家庭總收入:

60萬以下:貧窮階級 171萬9196戶 (22.79%)

61~100萬:新貧感強烈的中下階級 203萬2004戶 (26.93%)

101~199萬:焦慮的中產階級 300萬5102戶 (39.83%)

200萬以上:富裕的中上階級 78萬8102戶 (10.45%)

2011/11/21

Notepad++的Virtual Space(虛擬空間)

設定方式相當簡單:
1.安裝Python Script plugin
2.samples->Enable / Disable Virtual Space

Open-dbExpress最近又更新囉!

現在的檔案為

dbxoodbc_3_2011_1119dev1.7z  
dbxoodbc_3_2011_1129dev1.7z
dbxoodbc_3_2012_0716dev.7z

這個版本更新了
ExecSQL的Bug
支援 RAD Studio XE2了

越來越完美啦!

哈哈

2011/11/11

[轉]Flex 4 addChild 不能用了?


因为项目所需,要在Sprite上使用画图API并且将其显示,采用了以前的方法:
也就是在Sprite上画东西,然后用UIComponent.addChild(Sprite),再addChild(UIComponent)
import mx.core.UIComponent;
import mx.events.FlexEvent;
private var spr:Sprite;
private var uic:UIComponent;
protected function init(event:FlexEvent):void
{
  // TODO Auto-generated method stub
  spr = new Sprite;
  uic = new UIComponent;
  spr.graphics.beginFill(0xFFFFFF);
  spr.graphics.drawCircle(100, 100, 100);
  spr.graphics.endFill();
  uic.addChild(spr);
  addChild(uic);
}

2011/11/01

Flex 4 – FlexGlobals.topLevelApplication

在Flex 3读取HTML页面的flashvars的变量需要使用Application.application,在Flex 4 SDK里使用FlexGlobals.topLevelApplication代替了Application.application。 

Flex 4已经使用swfobject了: 


<script type="text/javascript"> 
    var swfVersionStr = "10.0.0"; 
    var xiSwfUrlStr = "playerProductInstall.swf"; 
    var flashvars = {}; 
    flashvars.firstname = "Nick"; 
    flashvars.lastname = "Danger"; 
    var params = {}; 
    params.quality = "high"; 
    params.bgcolor = "#ffffff"; 
    params.allowscriptaccess = "sameDomain"; 
    var attributes = {}; 
    attributes.id = "TestProject"; 
    attributes.name = "TestProject"; 
    attributes.align = "middle"; 
    swfobject.embedSWF( 
        "FlashVarTest.swf", "flashContent", "100%", "100%", swfVersionStr, 
        xiSwfUrlStr, flashvars, params, attributes); 
    swfobject.createCSS("#flashContent", "display:block;text-align:left;"); 
script>

读取flashvars中firstname和lastname变量的写法: 

var first_name : String = FlexGlobals.topLevelApplication.parameters.firstname;
var last_name : String = FlexGlobals.topLevelApplication.parameters.lastname;

在Flash Builder 4里用Flex 4 SDK开发时,如果还使用了Application.application,则会有提示这是Flex 3 SDK的写法。


文章來源:Flex 4 – FlexGlobals.topLevelApplication

2011/10/13

delphi多线程TThread类介绍

Delphi中有一个线程类TThread是用来实现多线程编程的,这个绝大多数Delphi书藉都有说到,但基本上都是对TThread类的几个成员作一简单介绍,再说明一下Execute的实现和Synchronize的用法就完了。然而这并不是多线程编程的全部,我写此文的目的在于对此作一个补充。
    线程本质上是进程中一段并发运行的代码。一个进程至少有一个线程,即所谓的主线程。同时还可以有多个子线程。当一个进程中用到超过一个线程时,就是所谓的“多线程”。
    那么这个所谓的“一段代码”是如何定义的呢?其实就是一个函数或过程(对Delphi而言)。
    如果用Windows API来创建线程的话,是通过一个叫做CreateThread的API函数来实现的,它的定义为:

2011/09/28

Indy -- 專業的Blocking

Indy (Internet Direct)與Dbexpress在Delphi中是我最喜歡的技術類型。
今天來說說它的特色之一 -- Blocking

很多人都會很好奇,SocketServer和idTCPServer主要的差異在於一個預設為non-Blocking,另一個則是恆為Blocking,但這兩者之間有什麼樣的不同呢?

我不知道!(被毆)

2011/09/15

DBX4MySQL - 極緻效能的 Dbexpress Driver

鮮少使用MySQL,直到有人問我Dbexpress如何連接MySQL時,發現這比想像中要來得困難。

在尋找解決方案時,突然發現這個好物:DBX4MySQL (DBX for MySQL)

它的概念和Devart的MyDAC很像
MyDAC Connection Flow [Direct Mode] (節錄自Devart官網)
MicroOLAP官方是這樣寫的:
Any MySQL libraries are not requred on a client workstation, even libmysql.dll

2011/09/14

特殊符號的英文單字

資料來源:身為一個專業的程式設計師需具備的基本知識(一)

/ Slash (斜線)
\ Backslash (反斜線)
~Tilde (取代符號)
@ At sign, at (At 符號,At)

# Poundsign (井字號)

$ Dollarsign (錢符號)

^ Caret (插入號)

& Ampersand (And 符號)

* Asterisk (星號)

[ Open bracket (左開式方括弧)

] Close bracket (右關式方括弧)

( Open parenthesis (左開式圓括號)

) Close parenthesis (右關式圓括號)

: Colon (冒號)

, Comma (逗號)

-- Double dash (雙破折號)

... Ellipsis (省略符號)

' Single quote (單引號)

" Quote (引號)

= Equals (等號)

+ Plus,plus sign (加,加號)

! Exclamation point (驚歎號)

> Greater than (大於)

< Less than (小於)

? Question mark (問號)

. Period,dot (句號,點)

; Semicolon (分號)

- Hyphen (連字號)

— Dash (破折號)

_ Underscore (底線)

| Vertical bar (垂直線)

{ Open brace (左開式大括號)

} Close brace (右關式大括號)

% Percent,percent sign (百分比,百分比符號)

// Double slash (雙斜線)

2011/09/13

[轉]FLEX AIR 儲存路徑的三種選擇


資料來源:AIR 文件存储路径三种选择

1.File.desktopDirectory
路径参数:C:\Documents and Settings\当前用户名\桌面
file = File.desktopDirectory.resolvePath("HelloWorld.txt");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
var str:String = "Congratulations on AIR Save Path ";
stream.writeUTFBytes(str);
stream.close();
2.File.applicationStorageDirectory
路径参数:C:\Documents and Settings\当前用户名\Application Data\应用程序项目名\Local Store
var file:File = File.applicationStorageDirectory.resolvePath("HelloWorld.txt");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
var str:String = "Congratulations on AIR Save Path";
stream.writeUTFBytes(str);
stream.close();

3.File.applicationStorageDirectory
路径参数:C:\Documents and Settings\当前用户名\My Documents
var file:File = File.documentsDirectory.resolvePath("HelloWorld.txt");
var stream:FileStream = new FileStream()
stream.open(file, FileMode.WRITE);
var str:String = "Congratulations on AIR Save Path";
stream.writeUTFBytes(str);
stream.close();

2011/09/01

SQL Pretty Printer:好用的 SQL Formatter 工具

目前使用 SSMS,不論是2005還是2008,都沒有SQL Formatter功能,往往看到長得天荒地老的一行SQL,總是要費盡心力地來無盡的ENTER ENTER ENTER ENTER......

在網路上找了找,有個方便的工具:Instant SQL Formatter
線上格式化SQL工具,還蠻方便的,但並不支援UNICODE,所以出現非英文語時就會出現「???」,還真的是麻煩...
中文亂碼的畫面
後來才發現,它有單機版本,名稱叫:SQL Pretty Printer
有提供 Desktop version, Add-In for SSMS, Add-In for VS or API
API就是利用網路連結到Instant SQL Formatter來處理。所以重點還是在前三項

因為我不只是用MS SQL,所以下載了Desktop Version來測試,以下就是展示畫面:
未格式化時的SQL
格式化後的SQL

更棒的是,這個版本就支援UNICODE了!

工作上經常會先將 T-SQL 測到沒有問題後,再謄到 Delphi 中,此時會進行字串相加的串連工作,然而這工作如果文字少還算簡單,如果量大時,難免會出現漏打的情形,這時 SQL Pretty Printer 也支援 SQL 指令碼轉成其它如 C#、Pascal 相容的字串相加程式碼:

SQL Pretty Printer 有將 SQL 組成其它語言字串相加的功能

價格

SQL Pretty Printer 是付費軟體,一個LICENSE $50,但為你省下的時間絕對超過 $50 的價值!

向你推薦!


General SQL Parser for VCL 簡介

最近這一陣子大量在Delphi存取MS SQL,對於SQL指令的解析及輸入的繁瑣實在是頭痛。

比方說:
SELECT a.name,
       b.id,
       c.class
FROM   a
       JOIN b
         ON a.name = b.name
            AND a.money > 100
       JOIN c
         ON c.x > a.y
WHERE  a.address NOT IN (SELECT *
                         FROM   xtable
                                JOIN ytable
                                  ON xtable.id <> ytable.id



這樣的語法,如果要輸入到Delphi裡,對coding人員來說,一定是場惡夢,更不要說那些複雜到見鬼的SQL指令了,所以我們常會這樣打:

str := 'SELECT A.NAME, B.ID, C.CLASS FROM A JOIN B ON A.NAME = B.NAME AND A.MONEY > 100 JOIN C ON C.X > A.Y WHERE A.ADDRESS NOT IN (SELECT * FROM XTABLE JOIN YTABLE ON XTABLE.ID <> YTABLE.ID'

中間完全不間斷………

要不就是認命地自己斷行,結果中間多打或少打一個字,也得等到執行時期才抓得到錯誤…

這時候就會覺得JQuery或LINQ真是好物啊!

在這邊介紹一個好物:General SQL Parser VCL Version

它算是一個外掛的CLASS,利用USES的方式引入到專案中即可使用
目前還在深入摸索中,所以我們先借用它內附的DEMO吧!
咦!出錯了!
上面的畫面可以看到我們KEY入的SQL有錯誤,也很明白的指出是缺少了「)」

這時我們再補上後執行,就會出現正確的結果了
正解!
當然,不會只是滿足於現有的Check syntax,所以我們還可以使用pretty print!
不只是單純的Memo,還可以搭配Rich Text
另外,還可以編成各語言的傳入字串:
剛剛的一行SQL轉成Delphi String型態,超方便!

但是這套工具價值不斐

2011/08/26

[轉]flex4.5中布局元素与布局约束的用法

其實 Flex 4.0 也可以參考使用。
以下是轉載資料。

來源:flex4.5中布局元素与布局约束的用法

在对容器元素布局与容器布局约束时,经常会用到horizontalAlign、verticalAlign和horizontalCenter、verticalCenter这两组对象,尤其是在spark中如果遇到可以同时设置这两组对象的容器时(如HGroup,VGroup)会有种混乱的感觉。
因此我们最好弄清它们的作用和用法
1、horizontalAlign、verticalAlign:
这一组对象是容器的属性,用来设置容器内的元素在水平和垂直方向的对齐方式,所以操作的对象是容器内的元素。在HGroup,VGroup和中存在这两个属性,但Group、Panel 和Application中没有这两个属性。
2、horizontalCenter、verticalCenter:
这一组对象是容器的样式,用来设置约束容器本身在父级容器中的位置,从组件(容器本身)中心到锚点目标(父级容器)的内容区域中心的距离,所以操作的对象是容器本身。在HGroup,VGroup,Panel 和Application中都存在这组样式。
需要注意的是,这组样式仅在 Canvas 容器中的组件上,或在 Panel 或 Application 容器(layout 属性设置为 absolute)中的组件上使用时才发挥作用。也就是说如果在spark中还必须保证Application的layout属性设置为absolute,即在spark中保留Application的layout属性为默认或设置为




<s:layout>
    <s:BasicLayout/>
</s:layout>



否则horizontalCenter、verticalCenter的样式设置将无效。
下面为一个测试例子:




<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="600" minHeight="400">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <s:VGroup width="50%" height="50%" horizontalAlign="center" verticalAlign="middle" horizontalCenter="0" verticalCenter="0">
        <s:Label text="label"/>
        <s:Button label="button"/>
    </s:VGroup>
    <s:Group width="20%" height="20%" horizontalCenter="0" verticalCenter="100">
        <s:Button label="g button"/>
    </s:Group>
</s:Application>

2011/08/16

Owner vs. Parent in Delphi Applications

Owner vs. Parent in Delphi Applications

很有意思的一段,在搜尋Panel裡所有的元件時,很有幫助:
for ii := 0 to Panel1.ControlCount - 1 do

   Panel1.Controls[ii].Visible := false;

2011/08/15

Tony's Blog: 寫程式的好習慣

Tony's Blog: 寫程式的好習慣: "photo by Graeme's Pix 每個程式設計師都有一些寫程式的習慣,有好的也有不好的,多看看一些別人的習慣,好的可以學習,不好的可以改進。以下我只有擷取原文部份的內容做翻譯,方便日後再次閱讀。星號表示我自己的一些想法。 5個寫程式的好習慣 (5 Good Pr..."

2011/08/13

[轉] Code Formatting in Flash Builder

資料來源:Code Formatting in Flash Builder


Follow these steps to set up automatic code formatting within flash builder.
Note that this is specific to your workspace so you should store the file somewhere on
your system and become familiar with the steps to initialize these tools.
The install process is the same as anyone installing plugins on Flash Builder.
If you are familiar with this process feel free to just utilize the links to the
software below.
Install Flex Formatter:

2011/08/11

你應該立即停止使用Delphi的十大趣味理由


1. Delphi makes it too easy to write good programs which leads to a decline in programmer billable hours.
使用Delphi寫程式真是太容易了,讓寫程式的工時減少了
Delphi讓寫出優質程式這回事變得太容易了,以至於讓程式設計師平白喪失了許多可資計費的時數

2011/07/29

令人崩潰的分頁預覽範圍…

Excel VBA中有個 RefersToRange屬性,可以正確得到可列印的最大範圍,MSDN是這樣寫的:
' RefersToRange 屬性範例
' 此範例顯示作用中工作表列印範圍中的列數和欄數。

p = Names("Print_Area").RefersToRange.Value
MsgBox "Print_Area: " & UBound(p, 1) & " rows, " & _
    UBound(p, 2) & " columns"


Delphi Excel Component要怎麼寫?

2011/07/28

[自製小軟體]SQL Server Description Editor

English content:
[SSDE Privacy]
  1. The "Description" comment on Table or Field in SQL Server. 
  2. Can be exported into Excel file for easy editing, and import after the updated the Excel file.
  3. Native mode app. Performance is high. 

=============================

使用IDE: Delphi XE
本專案包含的範圍:
dbExpress, ODBC Open Source DBX Driver, DataSnap ( DataSetProvider + ClientDataSet )

功能特色:兩可一沒有
  • 可編輯欄位中的「描述」欄位
  • 可匯出成Excel檔,方便編輯,編輯完後亦可匯入更新
  • 不需額外安裝 .Net 即可使用,非常輕薄

系統需求:
  • MS SQL Server 7, 2000, 2005, 2008 或以上
  • Office 2000 以上版本
  
比較特別的地方:
Connection Mode:
有 OleDB 和 SQL Client 兩種連結方法,只是先後順序的差別,沒有 SQL Client 時仍會切回 OleDB 連結

Demo PIC:




Download Exe version
2012/11/17補充:
在進行改版的時候發現了底下幾篇 -

下次來用這些工具試試

===========================================

SQL Server Description Editor Windows Store App - 隱私權宣告

我們在何時收集什麼樣的信息?
當您使用應用程序時,我們並不會向您收集任何信息,如您的所在位置或IP資訊等。

我們為什麼要使用你的信息呢?
當您使用應用程序時,我們並不會向您收集任何信息,故並不會有出售、交換、轉移、或未經您的同意的行為。

我們如何保護您的信息?
當您進入或使用本軟體時,我們實行的各種保安措施,會維護您的個人信息安全。

我們使用cookies?
我們不使用cookies。

我們向外界透露任何信息嗎?
我們不會出售與交易或以其他方式轉讓您的個人身份信息。

您的同意
通過使用我們的應用程式,您同意我們應用程式的隱私政策。

我們的隱私政策的更改
如果我們決定改變我們的隱私政策,我們將在此頁面上更新這些變化。

聯繫我們
如果對本隱私政策有任何疑問,您可以使用文末的留言與我們連繫。

[自製小軟體]XLS to MS SQL Server

使用IDE: Delphi 7
本專案包含的範圍:
ADO ExpressMicrosoft Office XP Sample Automation Server Wrapper Components

功能特色:
1. 簡易的 Data Pump 功能,將 Excel file 匯入到 SQL Server 內
2. 不需額外安裝 .Net 即可使用,非常輕薄

系統需求:
MS SQL Server 7 以上
Office 2000 以上

Demo PIC:















Download

2011/07/24

書評:主管私房學 自慢2--小職員出頭天

圖片來源:博客來書籍館
「自慢」:在日文中是指形容自己最拿手、最有把握、最專長的事。

這本書,真可謂道盡小職員心目中理想的好主管與壞主管,他們之間的差別。

這本書講得很多,也可以了解到這本書的作者是屬於急驚風的主管類型,我想,跟隨他的人應該都是步調很快而且很不拖泥帶水的吧。

2011/07/16

書評:Delphi in Depth: ClientDataSet

目前只大略的把這本書看過一次,如果看完第二次還有多的心得再補充一下

TClientDataSet真的是個很有潛力的元件

基本上,它就是BDE的延伸,很多語法和概念是繼承於它

它可以單打獨鬥,也可以多人合作(與Base DataSet搭配,如BDE、ADO、DBX等),相當萬用的一個元件,同時,它也是Midas / DataSnap架構的重要元件之一

要說它的缺點的話,應該就是它不支援SQL指令吧,例如像我想在已有資料的ClientDataSet裡做SQL查詢,就不能夠這樣使用

新手老手都能夠在這本書上看到很新鮮的東西
比方說:
新手可以從第一章看到第十章,進階在十一~十二章
老手可能就是看看十三~十五章,比較DataSnap新舊版本的差異和設計方針
(重點就是放在JSON啦!)
同時也能當工具書隨手翻翻

九到十二章是我相當少用的功能(好吧,我承認,我從來沒用過)
未來有機會我仍然會來挑戰這個功能,榨光ClientDataSet的極限吧!

2011/07/12

TExcelWorksheet刪除列的方式

真是見鬼的做法,Google半天居然沒有網站可以解決這問題

首先,如果我要把Excel工作表的D欄刪除,在Excel VBA是這樣寫的:

Sub Macro2()
    Columns("D:D").Select
    Selection.Delete Shift:=xlToLeft
End Sub


找找找,怎麼找都是顯示ComObj的處理,沒有人用Delphi內建的元件
剛好我偏偏使用了:

2011/07/05

Multithreaded Delphi Database Queries with dbExpress (DBX)

This article is extended the content of Multithreaded Delphi Database Queries, which is mainly dbGO(ADO) change to Dbexpress(DBX).

The same opinion is as below mentioned:
1.      Solve: “Canvas does not allow drawing”.
2.      Main TSQLConnection Cannot be used!

And the superiority of DBX is:
1.      DBX is read only and unidirectional.
2.      Device is very flexible.

Because SQLDataSet’s property always ‘string’ and need add SQLConnection, so we can modify this code:
Multithreading in Dbexpress (DBX)

Let's say you want to display orders for 3 selected customers in a Delphi list box control.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
  TCalcThread = class(TThread)
  private
    procedure RefreshCount;
  protected
    procedure Execute; override;
  public
    ConnStr : string;
    SQLString : string;
    cnThread: TSQLConnection;
    ListBox : TListBox;
    Priority: TThreadPriority;
    TicksLabel : TLabel;

    Ticks : Cardinal;
  end;

Well, DBX is not support ACCESS in Delphi default setting. So we used “Open Source ODBC Dbexpress driver” in the project. And add it’s demo: dbx_access_connect.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
procedure TDbxThreadedForm.ReConnection();
begin
    //
    // MSACCESS
    //
    dbx_access_connect.AccessConnect(SQLConnection,
      // mdb_file_name:
        'C:\Program Files\Common Files\CodeGear Shared\Data\dbdemos.mdb',
      // DNS_NAME
        '',
      // DirectOdbc
        True,
      // LoginPrompt
        False,
      // UserName
        '',
      // Password
        '',
      // AdditionalOptions
        'coEnableBCD=0',
      // bUnicodeOdbcApi
        True,
      // bAnsiStringField
        False,
      // bUnicodeDriver
        True
    );
end;

When the 3 customers are selected from the drop down box, we create 3 instances of the CalcThread:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
procedure TDbxThreadedForm.Button1Click(Sender: TObject);
var
  s, sg: string;
  c1, c2, c3 : integer;
begin
  s := ' SELECT O.SaleDate, MAX(I.ItemNo) AS ItemCount  ' +
       ' FROM Customer C, Orders O,  Items I   ' +
       ' WHERE C.CustNo = O.CustNo AND I.OrderNo = O.OrderNo ' ;

  sg := ' GROUP BY O.SaleDate ';

  c1 := Integer(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
  c2 := Integer(ComboBox2.Items.Objects[ComboBox2.ItemIndex]);
  c3 := Integer(ComboBox3.Items.Objects[ComboBox3.ItemIndex]);

  Caption := '';

  ct1 := RunThread(Format('%s AND C.CustNo = %d %s',[s, c1, sg]), lbCustomer1, tpTimeCritical, lblCustomer1);

  ct2 := RunThread(Format('%s AND C.CustNo = %d %s',[s, c2, sg]), lbCustomer2, tpNormal,lblCustomer2);

  ct3 := RunThread(Format('%s AND C.CustNo = %d %s',[s, c3, sg]), lbCustomer3, tpLowest, lblCustomer3);
end;

Traps and Tricks - Multithreaded DBX Queries

The main code goes in the thread's Execute method:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
procedure TCalcThread.Execute;
  var
    Qry : TSQLDataSet;
    i : integer;
  begin
    inherited;
 
    ticks := GetTickCount();
    //CoInitialize(nil); //CoInitialize was not called
    cnThread := DbxThreadedForm.SQLConnection.CloneConnection;
    Qry := TSQLDataSet.Create(nil);
    try
      Qry.SQLConnection := cnThread;
      Qry.CommandText := SQLString;
 
      ListBox.Clear;
      Qry.Open;
      for i:= 0 to 100 do
      begin
        while NOT Qry.Eof and NOT Terminated do
        begin
          ListBox.Items.Insert(0, Format('%s - %d', [Qry.Fields[0].asString,Qry.Fields[1].AsInteger]));
 
          //Canvas Does NOT Allow Drawing if not called through Synhronize
          Synchronize(RefreshCount);
 
          Qry.Next;
        end; //while
        if Terminated then break;
        Qry.First;
        ListBox.Items.Add('*---------*');
      end; // for
    finally
      Qry.Free;
          FreeAndNil(cnThread);
    end;
    //CoUninitialize();
    ticks := GetTickCount - ticks;
    TicksLabel.Caption := 'Ticks: ' + IntToStr(ticks);
  end; //TCalcThread.Execute;

Conclusion
DBX will auto be called CoInitialize and CoUninitialize. So we can to comment it. :P

Attention: Similarly, you cannot use the TSQLConnection object from the main thread (application). (Remember free the TSQLConnection object)

You must use the Synchronize procedure to "talk" to the main thread and access any controls on the main form.

Be sure to download the demo application to explore and learn more about multithreaded queries with DBX and Delphi.

Source: