0% found this document useful (0 votes)
153 views2 pages

Conexão Com o Banco de Dados Classe Conexao

The document describes how to connect to a SQL database in C# and perform basic CRUD (create, read, update, delete) operations. It defines a Conexao class with methods to connect to and disconnect from the database. It shows examples of inserting a new record by executing a SQL INSERT statement, retrieving all records with a SELECT statement, and searching for a record by criteria with a parameterized SELECT statement. The records are loaded into a DataSet and bound to a data grid for display.

Uploaded by

cass78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views2 pages

Conexão Com o Banco de Dados Classe Conexao

The document describes how to connect to a SQL database in C# and perform basic CRUD (create, read, update, delete) operations. It defines a Conexao class with methods to connect to and disconnect from the database. It shows examples of inserting a new record by executing a SQL INSERT statement, retrieving all records with a SELECT statement, and searching for a record by criteria with a parameterized SELECT statement. The records are loaded into a DataSet and bound to a data grid for display.

Uploaded by

cass78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Conexo com o Banco de Dados

Classe Conexao
SqlConnection con = new SqlConnection(@Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BD_escolas;Data Source=LAB31-MICRO-14);
public SqlConnection Conectar()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
return con;
}
else
{
return con;
}
}
public SqlConnection Desconectar()
{
if (con.State == ConnectionState.Open)
{
con.Close();
return con;
}
else
{
return con;
}
}

Inserindo registros
conexao conn = new conexao();
SqlCommand cmd = new SqlCommand(insert into tb_professor (nome_prof, disc_prof, tel_prof, cel_prof) VALUES (' + nome_prof.Text + ', ' + disc_prof.Text + ', ' + tel_prof.Text + ', ' + cel_prof.Text + '), conn.Conectar());

cmd.ExecuteNonQuery();
conn.Desconectar();

Consultar Registros
Geral
conexao conn = new conexao();
SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM tb_professor, conn.Conectar());
DataSet ds = new DataSet();
da.Fill(ds);
dg_Consulta.DataSource = ds.Tables[0];
conn.Desconectar();
Por Critrio
conexao conn = new conexao();
SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM tb_aluno where cod_aluno=' + txt_Busca.Text + ', conn.Conectar());
DataSet ds = new DataSet();
da.Fill(ds);
dg_Consulta.DataSource = ds.Tables[0];
conn.Desconectar();

You might also like