Fórum Fazer pesquisa em banco sql e exibir no GRIDVIEW #66781
25/03/2009
0
Tiagom
Curtir tópico
+ 0Posts
26/03/2009
Pauloroger
Gostei + 0
26/03/2009
Pauloroger
No Uses coloco:
System.Data.SqlClient, //para trabalhar com SQLServer
Antes do [b:d1356c88a5]Implementation[/b:d1356c88a5] e coloco minha string de conexão, vc poderá trabalhar também junto ao Web.Config mas prefiro trabalhar assim, pois tenho vários servidores.
//CONEXAO COM O BANCO DE DADOS
const
strConexao = ´Data Source=LINUX3\SQLEXPRESS;Initial Catalog=BancoDeDados01;Persist Security Info=True;User ID=sa;Password=leaodejuda´;
Com Store Procdure:
var
Comand : SqlCommand;
Conn : SqlConnection;
SqlReader : SqlDataReader;
begin
Conn := SqlConnection.Create(strConexao);
Comand := SqlCommand.Create(´ProcuraPorNome´, Conn);
Comand.CommandType := CommandType.StoredProcedure;
Comand.Parameters.Add(´@NOME´, txtLocalizar.Text.ToUpper);
Conn.Open;
SqlReader := Comand.ExecuteReader;
try
GridView1.DataSource := SqlReader;
GridView1.DataBind;
finally
Conn.Close;
end;
//*************************************************
Usando SQL na Aplicação, depende da quantidade das sua requisições, eu uso mais para aplicações com Banco de Dados Firebird, porem poderá ser adaptada para SQLServer, veja exemplo:
var
Comand: FbCommand;
DataAdapter: FbDataAdapter;
Conn: FbConnection;
fbReader : FbDataReader;
prNOME : FbParameter;
begin
{ Criação dos objetos de conexão }
Conn := FbConnection.Create;
DataAdapter := FbDataAdapter.Create;
Comand := FbCommand.Create;
{ Atribuição da string de conexão e abertura do BD}
Conn.ConnectionString := strConexao;
Conn.Open;
{ Atribuição dos atributos de seleção dos dados }
DataAdapter.SelectCommand := Comand;
DataAdapter.SelectCommand.Connection := Conn;
DataAdapter.SelectCommand.CommandText :=
´SELECT A.COD_CLIENTE, A.NOME, A.ENDERECO, A.FONE, A.DATA_NASC, A.CPF FROM CLIENTES A ´ +
´WHERE (A.NOME like ?) ORDER BY A.NOME´;
prNOME := FbParameter.Create;
DataAdapter.SelectCommand.Parameters.Add(prNOME);
DataAdapter.SelectCommand.Parameters[0].Value := ´¬´ + txtLocalizar.Text + ´¬´;
fbReader := DataAdapter.SelectCommand.ExecuteReader;
{ Criaçã em memória do DataSet auxliar }
try
gridClientes.DataSource := fbReader;
gridClientes.DataBind;
finally
Conn.Close;
end;
Use sua imaginação agora, ok.
Um abração.
Gostei + 0
26/03/2009
Tiagom
Gostei + 0
26/03/2009
Tiagom
(var
Comand : SqlCommand;
Conn : SqlConnection;
SqlReader : SqlDataReader;
begin
Conn := SqlConnection.Create(strConexao);
Comand := SqlCommand.Create(´ProcuraPorNome´, Conn);
Comand.CommandType := CommandType.StoredProcedure;
Comand.Parameters.Add(´@NOME´, txtLocalizar.Text.ToUpper);
Conn.Open;
SqlReader := Comand.ExecuteReader;
try
GridView1.DataSource := SqlReader;
GridView1.DataBind;
finally
Conn.Close;
end;)
Obrigado.
Gostei + 0
26/03/2009
Pauloroger
No Uses coloco:
System.Data.SqlClient, //para trabalhar com SQLServer
Antes do Implementation e coloco minha string de conexão, vc poderá trabalhar também junto ao Web.Config mas prefiro trabalhar assim, pois tenho vários servidores.
//CONEXAO COM O BANCO DE DADOS
const
strConexao = ´Data Source=LINUX3\SQLEXPRESS;Initial Catalog=BancoDeDados01;Persist Security Info=True;User ID=sa;Password=leaodejuda´;
Com Store Procdure:
No botão:
procedure TCadClientes.btProcurar_Click(sender: System.Object; e: System.EventArgs);
var
Comand : SqlCommand;
Conn : SqlConnection;
SqlReader : SqlDataReader;
begin
Conn := SqlConnection.Create(strConexao);
Comand := SqlCommand.Create(´ProcuraPorNome´, Conn);
Comand.CommandType := CommandType.StoredProcedure;
Comand.Parameters.Add(´@NOME´, txtLocalizar.Text.ToUpper);
Conn.Open;
SqlReader := Comand.ExecuteReader;
try
GridView1.DataSource := SqlReader;
GridView1.DataBind;
finally
Conn.Close;
end;
end;
No SQLServerExpress2005 crie um procedure assim:
-- =============================================
-- Author:Paulo Freire
-- Create date: 23/11/2006
-- Description:Procura Cliente por Nome
-- =============================================
CREATE PROCEDURE ProcuraPorNome (
@NOME VARCHAR(60)
)
AS
SELECT * FROM CLIENTES
WHERE CLIENTES.NOME LIKE ´¬´ + @NOME + ´¬´
Gostei + 0
26/03/2009
Tiagom
Gostei + 0
27/03/2009
Pauloroger
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)