aew galera tudo bom
estava eu a pensar sobre como fazer algo mais rapido para conectar o banco e montei essa classe abaixo para tornar mais agil esse processos
caso alguem queira segue abaixo
não está completa ainda mas já é um começo para quem está perdido
quem poder contribuir agradeço
fiz ja algumas modificações desde que eu postei essa msg
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe; //this Reference you need to Add in the project
using System.Windows.Forms;
using System.Data;
//this namespace is modify following the ApplicationName
namespace DeviceApplication21
{
class Banco
{
// ---------------Begin Of Declarations of parameters ------------- //
//The name of the Database
private String DataBaseName;
//The Password of the Database
private String PassDataBase;
//connections to the database
private SqlCeConnection conn;
//Command SQL
private SqlCeCommand cmd;
//Needed to receive the data
private SqlCeDataAdapter da;
//Table selected of the Data
private DataTable dt;
//The Row selected of the Data
private DataRow rw;
// ---------------End Of Declarations of parameters ------------- //
//constructorof the Classe class
public Banco(String DBname, String Pass)
{
this.setDataBaseName(DBname);
this.setPassDataBase(Pass);
}
//Getter and Setter to DataBase Settings
public void setDataBaseName(String DBName)
{
this.DataBaseName = DBName;
}
private String getDataBaseName()
{
return this.DataBaseName;
}
//Getter and Setter to DataBase Settings
public void setPassDataBase(String Pass)
{
this.PassDataBase = Pass;
}
private String getPassDataBase()
{
return this.PassDataBase;
}
//create a String connection
private void InicializeDB()
{
conn = new SqlCeConnection("Data Source ="
+ (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
+ ("\\"+ this.getDataBaseName() +";" + ("Password =" + "\""+ this.getPassDataBase() +"\";"))));
}
//Open the connection of the database
private void openConnection()
{
try
{
InicializeDB();
this.conn.Open();
}
catch(Exception e)
{
MessageBox.Show("Problemas na Conexão do Banco de Dados Erro: " + e.ToString(),"Atenção!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
}
}
//Select all data of the table
public DataTable selectAllDataOfTable(String Table)
{
openConnection();
//string to select the data
this.cmd = new SqlCeCommand("Select * from " + Table , this.conn);
//execute the sql
this.cmd.ExecuteNonQuery();
//return the data select
this.da = new SqlCeDataAdapter(this.cmd);
this.dt = new DataTable();
this.da.Fill(dt);
this.conn.Close();
return this.dt;
}
//select all data of select with WHERE condition
public DataTable selectDataOf(String Table,String Where)
{
openConnection();
//string to select the data
this.cmd = new SqlCeCommand("Select * from " + Table + " WHERE " + Where, this.conn);
//execute the sql
this.cmd.ExecuteNonQuery();
//return the data select
this.da = new SqlCeDataAdapter(this.cmd);
this.dt = new DataTable();
this.da.Fill(dt);
this.conn.Close();
return this.dt;
}
//update data in the table following the current row
public void UpdateData(DataSet dataset,String Table,int currentRow, String Set, String Where)
{
openConnection();
rw = dataset.Tables[Table].Rows[currentRow];
this.cmd = new SqlCeCommand("UPDATE " + Table + " " +
"SET "+ Set +" " +
"WHERE ( " + Where + " )", this.conn);
this.cmd.ExecuteNonQuery();
MessageBox.Show("Registro Atualizado com Sucesso!","Atualizado!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
this.conn.Close();
}
//insert a new data in the table
public void InsertData(String Table,String Fields,String Values)
{
openConnection();
this.cmd = new SqlCeCommand("INSERT INTO "+ Table +" " +
"( "+ Fields +" ) " +
"VALUES ( " + Values + " )", this.conn);
this.cmd.ExecuteNonQuery();
MessageBox.Show("Registro Salvo com Sucesso!", "Salvo!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
this.conn.Close();
}
//delete the data select in where condition
public void DeleteData(String Table,String Where)
{
openConnection();
this.cmd = new SqlCeCommand("DELETE FROM "+ Table +" " +
"WHERE ( " + Where + " )", this.conn);
this.cmd.ExecuteNonQuery();
MessageBox.Show("Registro Deletado com Sucesso!", "Deletado!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
this.conn.Close();
}
//execute all SQL (this method are make to give a free use in condition SQL)
public void ExecuteSQL(String SQL)
{
openConnection();
this.cmd = new SqlCeCommand(SQL, this.conn);
this.cmd.ExecuteNonQuery();
this.conn.Close();
}
//execute all SQL (this method are make to give a free use in condition SQL) and return a message
public void ExecuteSQL(String SQL, String MSG, String Title, MessageBoxButtons Buttons, MessageBoxIcon Icon, MessageBoxDefaultButton DefaultButton)
{
openConnection();
this.cmd = new SqlCeCommand(SQL, this.conn);
this.cmd.ExecuteNonQuery();
MessageBox.Show(MSG, Title, Buttons, Icon, DefaultButton);
this.conn.Close();
}
//execute all SQL (this method are make to give a free use in condition SQL)and return a DataSelection
//Only used to SelectCondition
public DataTable ExecuteSelect(String SQL)
{
openConnection();
//string to select the data
this.cmd = new SqlCeCommand(SQL, this.conn);
//execute the sql
this.cmd.ExecuteNonQuery();
//return the data select
this.da = new SqlCeDataAdapter(this.cmd);
this.dt = new DataTable();
this.da.Fill(dt);
this.conn.Close();
return this.dt;
}
}
}