Solved

Delphi and Insert, update & Delete statement using SQL

Posted on 2004-08-01
7
1,722 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now