Update usando o datagrid
A mesma idéia serve para o insert e delete, basta implementar
...
private
{ Private Declarations }
public
procedure AbriConsulta();
PROCEDURE ABRIRREGISTRO(idaluno : STRING);
{ Public Declarations }
end;
implementation
{$REGION 'Designer Managed Code'}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///
procedure TWebForm1.InitializeComponent;
begin
Self.SqlConnection1 := System.Data.SqlClient.SqlConnection.Create;
Self.SqlCommand1 := System.Data.SqlClient.SqlCommand.Create;
Self.SqlCommand2 := System.Data.SqlClient.SqlCommand.Create;
Self.SqlComd := System.Data.SqlClient.SqlCommand.Create;
Include(Self.DataGrid1.ItemCommand, Self.DataGrid1_ItemCommand);
Include(Self.Button1.Click, Self.Page_Load);
Include(Self.Button2.Click, Self.Button2_Click);
//
// SqlConnection1
//
Self.SqlConnection1.ConnectionString := 'user id=sa;data source=ALFREDO;pe' +
'rsist security info=False;initial catalog=teste';
//
// SqlCommand1
//
Self.SqlCommand1.CommandText := 'select * from alunos';
Self.SqlCommand1.Connection := Self.SqlConnection1;
//
// SqlCommand2
//
Self.SqlCommand2.CommandText := 'update alunos set nome = @nome where id_a' +
'luno = @id_aluno';
Self.SqlCommand2.Connection := Self.SqlConnection1;
Self.SqlCommand2.Parameters.Add(System.Data.SqlClient.SqlParameter.Create('@' +
'id_aluno', System.Data.SqlDbType.Int, 0, 'id_aluno'));
Self.SqlCommand2.Parameters.Add(System.Data.SqlClient.SqlParameter.Create('@' +
'nome', System.Data.SqlDbType.VarChar, 50, 'nome'));
//
// SqlComd
//
Self.SqlComd.CommandText := 'select * from alunos where id_aluno = @pid_al' +
'uno';
Self.SqlComd.Connection := Self.SqlConnection1;
Self.SqlComd.Parameters.Add(System.Data.SqlClient.SqlParameter.Create('@pi' +
'd_aluno', System.Data.SqlDbType.Int, 0, 'id_aluno'));
Include(Self.Load, Self.Page_Load);
end;
{$ENDREGION}
procedure TWebForm1.Page_Load(sender: System.Object; e: System.EventArgs);
begin
if (not isPostBack) then
AbriConsulta();
// TODO: Put user code to initialize the page here
end;
procedure TWebForm1.OnInit(e: EventArgs);
begin
// Required for Designer support
InitializeComponent;
inherited OnInit(e);
end;
procedure TWebForm1.AbriConsulta;
var
dr : SqlDataReader;
begin
SqlConnection1.Open();
try
dr := SqlCommand1.ExecuteReader();
datagrid1.DataSource := dr;
//pega o valor do id_aluno no grid
datagrid1.DataKeyField := 'id_aluno';
datagrid1.DataBind();
finally
SqlConnection1.close();
end;
end;
procedure TWebForm1.DataGrid1_ItemCommand(source: System.Object; e: System.Web.UI.WebControls.DataGridCommandEventArgs);
var
idaluno : string;
begin
// se o opção selecionar for escolhida chama ABRIRREGISTRO passando o parametro
if (e.CommandName = 'select') then
begin
idaluno := datagrid1.DataKeys[e.Item.ItemIndex].ToString();
ABRIRREGISTRO(idaluno);
end;
end;
procedure TWebForm1.Button2_Click(sender: System.Object; e: System.EventArgs);
// Botão que realiza o UPDATE na TABELA
begin
SqlConnection1.Open();
try
SqlCommand2.Parameters['@id_aluno'].Value := textbox1.Text;
SqlCommand2.Parameters['@nome'].Value := textbox2.Text;
SqlCommand2.ExecuteNonQuery();
finally
SqlConnection1.close();
end;
AbriConsulta();
end;
procedure TWebForm1.ABRIRREGISTRO(idaluno : STRING);
var
dr : SqlDataReader;
begin
SqlConnection1.Open();
try
SqlComd.Parameters['@pid_aluno'].Value := idaluno;
dr := SqlComd.ExecuteReader();
dr.Read();
textbox1.Text := dr['id_aluno'].ToString();
textbox2.Text := dr['nome']as string;
dr.Close();
finally
SqlConnection1.close();
end;
end;
end.
Por: Fabio Correa