Jump to content

[Resolvido] Conexão Usando Class


leoretorico

Recommended Posts

Boa noite amigos,sou novo em c# e normalmente para conectar com postgreSql,Acess e sqlServer eu uso o código direto no form load e nos buttons(delete,update e insert)

Gostaria muito que alguém pudesse me indicar um tutorial,projeto para download ou me mostrar um exemplo de uma classe de conexão entre c# e algum banco de dados.

Pois com CLASS o código fica mais organizado...

obrigado

Link to comment
Share on other sites

Amigo,

Pelo que esta falando acredito que estás a usar um linguagem orientada à objetos de forma procedural, portanto antes de pedir um exemplo de classe que implementa uma conexão à database, seria interessante estudar orientação à objetos.

Mas aqui tem um exemplo ( meio bagunçado, mas da para entender ): http://www.antaresinfo.blogspot.com.br/2008/08/criando-uma-classe-de-conexo-no-c.html

Link to comment
Share on other sites

Podes usar odbc, so' nao sei se funciona com postgre

Por muito mais que que estude só aprendo uma coisa, que ainda tenho muita coisa para aprender.

A beleza de um código está em decompor problemas complexos em pequenos blocos simples.

"learn how to do it manually first, then use the wizzy tool to save time."

"Kill the baby, don't be afraid of starting all over again. Fail soon, learn fast."

Link to comment
Share on other sites

como eu devo implementar um select(select * from tbl_links) no código abaixo?

e gostaria de saber como posso no form load informa o estado da conexão ! ex messagebox.show=" conexão "+connection.state

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace RHSever
{
class Connection
{
	public static SqlConnection conexao = new SqlConnection();

public static void AbrirConexao()
{
conexao.ConnectionString = @"Server=Dta-PC;Database=DataLink;UID=Leo;PWD=******";
conexao.Open();
}

public static void FecharConexao()
{
if (conexao.State == ConnectionState.Open)
{
conexao.Close();
conexao.Dispose();
}
}

public static void ExecutarComando(string textoComando)
{
if (conexao.State != ConnectionState.Open)
{
AbrirConexao();
}
SqlCommand comando = new SqlCommand ();
comando.CommandText = textoComando;
comando.Connection = conexao;
int registrosAfetados = comando.ExecuteNonQuery();
}

public static SqlDataReader SelecionarRegistros(string textoComando)
{
if (conexao.State != ConnectionState.Open)
{
AbrirConexao();
}
SqlCommand comando = new SqlCommand () ;
comando.CommandText = textoComando;
comando.Connection = conexao;
return comando.ExecuteReader();
}
public static SqlDataReader SelecionarUmRegistro(string textoComando)
{
if (conexao.State != ConnectionState.Open)
{
AbrirConexao();
}
SqlCommand comando = new SqlCommand();
comando.CommandText = textoComando;
comando.Connection = conexao;
return (SqlDataReader)comando.ExecuteScalar();
}

}
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.