Solved

Delphi and Insert, update & Delete statement using SQL

Posted on 2004-08-01
7
1,965 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…

738 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