Solved

Delphi and Insert, update & Delete statement using SQL

Posted on 2004-08-01
7
1,818 Views
Last Modified: 2010-04-05
Hi,
I am a VB programmer and just shifted to Delphi programming..I have Delphi 7 Enterprise edition in office and I want to develop an application by using ADO in it.

So can u please tell me what are the steps to use ADO and pass statements like INSERT, UPDATE AND DELETE to save, update and delete records from SQL SERVER database.

What components i need to drop on the form to achieve this goal and how to write sql statements?


Thanks.
0
Comment
Question by:nexustech
7 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 50 total points
ID: 11687537
Some examples cut from my application. Not complete.Access database.

//---------------------------------------------
  ADOConnection1.Connected := False;
  str := 'Provider=Microsoft.Jet.OLEDB.4.0;';
  str := str + 'Data Source='+
      IncludeTrailingPathDelimiter(ExtractFilePath( ParamStr(0) ))+'spectro3.mdb;';
  str := str + 'Persist Security Info=False';
  try
    ADOConnection1.ConnectionString := str;
    ADOConnection1.Connected := True;
//---------------------------------------------
....
//---------------------------------------------
      ADOConnection1.BeginTrans;
      try
        ADOCommand1.CommandText :=
          Format('DELETE FROM Hints WHERE (hin_subst_id=%d) AND (hin_no=%d)',
                                                        [sid,lp]);
        ADOCommand1.Execute;
        ADOCommand1.CommandText :=
          Format('UPDATE Hints SET hin_no = hin_no-1 WHERE (hin_subst_id=%d) AND (hin_no >%d)',
                                                        [sid,lp]);
        ADOCommand1.Execute;
        ADOConnection1.CommitTrans;
      except
        ADOConnection1.RollbackTrans;
        end;


//---------------------------------------------
....
//---------------------------------------------
          ADOCommand1.CommandText := Format('INSERT INTO Prof_lang (pla_profile_id,pla_lang_id) VALUES (%d,%d)',
              [pid,
              atLang.FieldByName('lang_id').AsInteger]);
          ADOCommand1.Execute;
0
 
LVL 6

Expert Comment

by:bpana
ID: 11687686
use ADOConnection for the connection. it has a propery ConnectionString (you can build the connectionstring on design-time by clicking on the button next to the property)
use ADOCommand to execute commands. (set it's Connection property to ADOConnection)

and check delphi help for each component.
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 11696466
Here's the link to a series of simple and easy-to-follow guide to database programming in Delphi (comes with screenshots as well) http://delphi.about.com/library/weekly/aa010101a.htm
0
 
LVL 1

Expert Comment

by:fidel83
ID: 12859839
hi nexustec, here's an easy way to do it. First you need to need to add ADODB to the 'uses' line at the top of the unit.


function runsql(sql: string; returnRS: boolean): _RecordSet;
begin
        try

        ADOQuery1:=TAdoQuery.Create(nil);
        ADOQuery1.Connection:=connection;

        if returnRS then begin
                ADOQuery1.SQL.text:=sql;
                ADOQuery1.Active:=true;
                result:=ADOQuery1.Recordset;
        end else begin
                ADOQuery1.SQL.text:=sql;

                result:=nil;
        end;

        ADOQuery1.Close;

        ADOQuery1.Free;

        except
                showmessage('error executing: ' + sql);

                connection.Connected:=false;
                connection.Connected:=true;
        end;
end;





procedure test;
var rs: _RecordSet;
begin
        rs:=runsql('select * from customer', true);
end;

procedure test2;
begin
        runsql('insert into customer (CUSTOMER_NUMBER) values (1000)', false);
end;
       
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question