Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi and Insert, update & Delete statement using SQL

Posted on 2004-08-01
7
Medium Priority
?
2,344 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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

886 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