Olá, tenho uma dúvida sobre qual método é o melhor para fazer uma conexão com um BD SQL Server.
Tenho esses dois métodos diferentes para fazer essa conexão com o BD.
Método 1:
public void CarregarRelatorio(string strSQL_atual, DataGridView dgv_atual, int tipo_de_bd)
{
dgv_atual.DataSource = null;
dt.Clear();
if (tipo_de_bd == 1)
{
connectionString = connectionStringDSR;
}
else if (tipo_de_bd == 2)
{
connectionString = connectionStringProjeto;
}
string strSQL = strSQL_atual;
objConnection = new SqlConnection(connectionString);
objCommand = new SqlCommand(strSQL, objConnection);
try
{
SqlDataAdapter objAdp = new SqlDataAdapter(objCommand);
objAdp.Fill(dt);
dgv_atual.DataSource = dt;
}
catch (Exception ex)
{
SystemSounds.Beep.Play();
MessageBox.Show(ex.Message);
}
}
Método 2:
public void CarregarRelatorio2(string strSQL_atual, DataGridView dgv_atual, int tipo_de_bd)
{
if (tipo_de_bd == 1)
{
connectionString = connectionStringDSR;
}
else if (tipo_de_bd == 2)
{
connectionString = connectionStringProjeto;
}
// Relatório de usuários do Projeto DSR.
string strSQL = @"SELECT * FROM Usuarios";
SqlDataAdapter objAdp = new SqlDataAdapter(objCommand);
BindingSource bsource = new BindingSource();
objConnection = new SqlConnection(connectionString);
objCommand = new SqlCommand(strSQL, objConnection);
try
{
objConnection.Open();
objAdp = new SqlDataAdapter(strSQL, objConnection);
DataSet ds = new DataSet();
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(objAdp);
objAdp.Fill(ds, "Usuarios");
bsource.DataSource = ds.Tables["Usuarios"];
dgv_atual.DataSource = bsource;
}
catch (Exception ex)
{
SystemSounds.Beep.Play();
MessageBox.Show(ex.Message);
}
}
Tenho dúvida sobre qual é melhor e quais as diferenças delas em questão de velocidade e segurança. Testei os dois e ambos funcionam. Estou criando uma classe para utilizar nos diversos forms, concentrando assim as principais funções em um local só.
Agradeço a ajuda :)