This tutorial describes how to modify data into tables using the TMSQuery and TMSTable components.
This walkthrough supposes that you know how to connect to server (tutorials "Connecting To SQL Server" and "Connecting To SQL Server Compact"), how to create necessary objects on the server (tutorial "Creating Database Objects"), and how to insert data to created tables (tutorial "Inserting Data Into Tables").
Data on server can be modified using Data Manipulation Language (DML), which is a part of SQL. DML statements can be executed on server by an account that has necessary privileges. There are two ways to manipulate a database. You can build DML statements manually and run them within some component like TMSQuery. Another way is to use the dataset functionality (the Edit and Post methods) of the TMSQuery and TMSTable components. We will discuss both ways. The goal of this tutorial is to modify the following record of the table dept:
10 | ACCOUNTING | NEW YORK |
to make it look as follows:
10 | RESEARCH | LOS ANGELES |
The Edit and Post methods of the TMSQuery and TMSTable components allow deleting data without using DML statements. DML statements are generated by SDAC components internally. The code below demonstrates using these methods:
[Delphi]
var
q: TMSQuery;
begin
q := TMSQuery.Create(nil);
try
// con is either TMSConnection or TMSCompactConnection already set up
q.Connection := con;
// retrieve data
q.SQL.Text := 'SELECT * FROM dept';
q.Open;
// to make the record with deptno=10 the current record
q.FindKey([10]);
// modify record
q.Edit;
q.FieldByName('dname').AsString := 'RESEARCH';
q.FieldByName('loc').AsString := 'LOS ANGELES';
q.Post;
finally
q.Free;
end;
end;
[C++Builder]
{
TMSQuery* q = new TMSQuery(NULL);
try
{
// con is either TMSConnection or TMSCompactConnection already set up
q->Connection = con;
// retrieve data
q->SQL->Text = "SELECT * FROM dept";
q->Open();
// to make the record with deptno=10 the current record
q->FindKey(ARRAYOFCONST((10)));
// modify record
q->Edit();
q->FieldByName("dname")->AsString = "RESEARCH";
q->FieldByName("loc")->AsString = "LOS ANGELES";
q->Post();
}
__finally
{
q->Free();
}
}
DML Statements can contain plain text and text with parameters. This section describes both ways.
[Delphi]
var
q: TMSQuery;
begin
q := TMSQuery.Create(nil);
try
// con is either TMSConnection or TMSCompactConnection already set up
q.Connection := con;
// set SQL query for update record
q.SQL.Clear;
q.SQL.Add('UPDATE dept SET dname = :dname, loc = :loc WHERE deptno = :deptno;');
// set parameters
q.ParamByName('deptno').AsInteger := 10;
q.ParamByName('dname').AsString := 'RESEARCH';
q.ParamByName('loc').AsString := 'LOS ANGELES';
// execute query
q.Execute;
finally
q.Free;
end;
end;
[C++Builder]
{
TMSQuery* q = new TMSQuery(NULL);
try
{
// con is either TMSConnection or TMSCompactConnection already set up
q->Connection = con;
// set SQL query for update record
q->SQL->Clear();
q->SQL->Add("UPDATE dept SET dname = :dname, loc = :loc WHERE deptno = :deptno;");
// set parameters
q->ParamByName("deptno")->AsInteger = 10;
q->ParamByName("dname")->AsString = "RESEARCH";
q->ParamByName("loc")->AsString = "LOS ANGELES";
// execute query
q->Execute();
}
__finally
{
q->Free();
}
}
[Delphi]
var
q: TMSQuery;
begin
q := TMSQuery.Create(nil);
try
// con is either TMSConnection or TMSCompactConnection already set up
q.Connection := con;
// set SQL query for update record
q.SQL.Clear;
q.SQL.Add('UPDATE dept SET dname = ''RESEARCH'', loc = ''LOS ANGELES'' WHERE deptno = 10;');
// execute query
q.Execute;
finally
q.Free;
end;
end;
[C++Builder]
{
TMSQuery* q = new TMSQuery(NULL);
try
{
// con is either TMSConnection or TMSCompactConnection already set up
q->Connection = con;
// set SQL query for update record
q->SQL->Clear();
q->SQL->Add("UPDATE dept SET dname = 'RESEARCH', loc = 'LOS ANGELES' WHERE deptno = 10;");
// execute query
q->Execute();
}
__finally
{
q->Free();
}
}
It is also possible to use stored procedures for modifying data. In this case, all data manipulation logic is defined on server. You can find more about using stored procedures in the tutorial "Stored Procedures".