Delphi 中使用公用的 ADODataSet

來源:互聯網
上載者:User

    在編寫資料庫系統的時候,少不了使用 ADODataSet 這個組件。在表不多的情況下,我們可以為每個表建立一個 ADODataSet,
然後通過 DBGrid 等資料顯示組件來操作她。

    但是,如果表的數量很多,比如多達幾十個表的時候,特別是表的數量動態變化的時候,我們就不太適合為每個表建立對應的 ADODataSet 了。

    這時,通過 ADODataSet 的複用,可以使問題大大簡化,減少大量的笨拙的代碼,使程式清晰而優美。甚至使一些無法編程的問題得到完美解決。

    具體做法:只在表單上拖放一個 ADODataSet 組件,然後在串連不同的表的事件中,使用以下類似代碼即可:

    ADODataSet1.Close;
    ADODataSet1.CommandText := 'Select * from <不同的表名>';
    ADODataSet1.Parameters.Clear;
    ADODataSet1.Parameters.ParseSQL(ADODataSet.CommandText, true);
    ADODataSet1.Open;

    以上代碼中,語句 ADODataSet1.Parameters.ParseSQL(ADODataSet.CommandText, true) 是關鍵代碼。通過 ParseSQL 方法解構 ADODataSet.CommandText 的 SQL 陳述式,建立 ADODataSet1 新的對象參數,從而使新的查詢生效。

    附:Delphi 文檔中對 ParseSQL 方法的描述:

    ParseSQL method (TParameters):

    Parses an SQL statement for parameters and TParameter objects.

    Delphi syntax:

    function ParseSQL(SQL: String; DoCreate: Boolean): String;

    Description

    Call ParseSQL to parse an SQL statement for parameters. For each parameter found in the SQL statement, a TParameter object is added to the calling TParameters object using the name of the parameter in the SQL statement.

    SQL contains the SQL statement to parse.

    DoCreate indicates whether to clear all existing parameter definitions before parsing the SQL statement.

    ParseSQL returns a parsed version of the SQL statement specified in the SQL parameter.

    ADOQuery1.Parameters.ParseSQL(ADOQuery1.Text, True);

    “十萬個為什麼”電腦學習網原創文檔
    轉載請註明出處:http://www.why100000.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.