domingo, 15 de maio de 2011

C# - Exemplo Conexão BD

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.Data.OleDb;





namespace Prophix___Controle_de_Saldos_e_Log_de_Execução

{

public partial class frmPrincipal : Form

{

// Variáveis Globais

string StringConexao, Senha;

SqlConnection Conexao;



public frmPrincipal()

{

InitializeComponent();

}



private void ConexaoBD_Open()

{

StringConexao = "Provider=sqloledb;Server=;Database=;User ID=RM;Password=;Trusted_Connection=False";

// Conexao = new SqlConnection(StringConexao);

}



private void Atualizar()

{

//SqlDataReader DadosDataHoraUltExec = QueryDtHoraUltExec.ExecuteReader();

OleDbConnection Conexao = new OleDbConnection(StringConexao);



//------------------------------------------------------------------//

//------------------------------------------------------------------//

// -- Aba "Data\Hora ----------------------------------------------//

//------------------------------------------------------------------//

//------------------------------------------------------------------//

string datahorainicial,

datahorafinal,

QueryDtHoraUltExecIni = "SELECT CONVERT(CHAR(10), DATAINICIAL, 103) + ' - '+CONVERT(CHAR(15), DATAINICIAL, 108) AS DATAINICIAL FROM ZRMPROPHIXLOGPROCESSOS WHERE PROCESSO = 0 AND PASSO = 0";



OleDbCommand SqlQueryDtHoraIni = new OleDbCommand(QueryDtHoraUltExecIni, Conexao);



SqlQueryDtHoraIni.CommandType = CommandType.Text;



OleDbDataAdapter DBAdapterQueryDtHoraIni = new OleDbDataAdapter(SqlQueryDtHoraIni);



DataTable QueryDadosDtHoraIni = new DataTable();



DBAdapterQueryDtHoraIni.Fill(QueryDadosDtHoraIni);



datahorainicial = Convert.ToString(QueryDadosDtHoraIni);



txtbDataHoraInicial.Text = datahorainicial;



//------------------------------------------------------------------//

//------------------------------------------------------------------//

// -- Aba "Data\Hora - Processos" ---------------------------------//

//------------------------------------------------------------------//

//------------------------------------------------------------------//

string QueryDtHoraUltExecProcesso = "SELECT * FROM ZRMPROPHIXLOGPROCESSOS";



OleDbCommand SqlQueryProcessos = new OleDbCommand(QueryDtHoraUltExecProcesso, Conexao);



SqlQueryProcessos.CommandType = CommandType.Text;



OleDbDataAdapter DBAdapterQueryProcessos = new OleDbDataAdapter(SqlQueryProcessos);



DataTable QueryDadosProcessos = new DataTable();



DBAdapterQueryProcessos.Fill(QueryDadosProcessos);



dataGridView1.DataSource = QueryDadosProcessos;





//, CONVERT(CHAR(10), DATAFINAL, 103) + ' - '+CONVERT(CHAR(15), DATAFINAL, 108) AS DATAFINAL

/*

DadosDataHoraUltExec.Read();



datahorainicial = (string)DadosDataHoraUltExec["DATAINICIAL"];

datahorafinal = (string)DadosDataHoraUltExec["DATAFINAL"];



txtbDataHoraInicial.Text = datahorainicial;

txtbDataHoraFinal.Text = datahorafinal;



DadosDataHoraUltExec.Close();



*/

}



private void Form1_Load(object sender, EventArgs e)

{

ConexaoBD_Open();

Atualizar();

}



private void textBox1_TextChanged(object sender, EventArgs e)

{

}



private void panel1_Paint(object sender, PaintEventArgs e)

{

}



private void tbUltExecProcessos_Click(object sender, EventArgs e)

{

}



private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

}



private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)

{

}

}

}