Solved

Delphi and Insert, update & Delete statement using SQL

Posted on 2004-08-01
7
2,084 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 3 hours left to enroll

617 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