I use the following form to make the connection.
class Conexao{
private const string _strCon = @"server=meuProvedor.com.br;" +
"user id=Meu_Login;" +
"password=Minha_Senha;" +
"database=Meu_banco;" +
"persistsecurityinfo=False";
private string vsql = "";
public MySqlConnection objCon = null;
#region "Métodos de conexão como o banco"
public bool conectar(){
objCon = new MySqlConnection(_strCon);
try{
objCon.Open();
return true;
}
catch{
return false;
}
}
public bool desconectar(){
if (objCon.State != ConnectionState.Closed){
objCon.Close();
objCon.Dispose();
return true;
}
else{
objCon.Dispose();
return false;
}
}
For consultations:
public DataTable ListaGrid(){
// Esta função lê a tabela Cliente e devolve para um DataGridView.
vsql = "SELECT * FROM CLIENTE ORDER BY NOME";
MySqlCommand objcmd = null;
if (this.conectar()){
try{
objcmd = new MySqlCommand(vsql, objCon);
MySqlDataAdapter adp = new MySqlDataAdapter(objcmd);
DataTable dt = new DataTable();
adp.Fill(dt);
return dt;
}
catch (MySqlException sqlerr){
throw sqlerr;
}
finally{
this.desconectar();
}
}
else{
return null;
}
}
public string Max(){
// Esta função busca o maior código de cliente e devolve numa string.
vsql = "SELECT MAX(CODCLIENTE) FROM CLIENTE";
MySqlCommand objcmd = null;
if (this.conectar()){
try{
objcmd = new MySqlCommand(vsql, objCon);
return Convert.ToString(objcmd.ExecuteScalar());
}
catch (MySqlException sqlerr){ throw sqlerr; }
finally{ this.desconectar(); }
}
else{ return "0"; }
}
public bool Update(ArrayList reg){
// Esta função faz um update na tabela Cliente e recebe parâmetros num ArrayList
vsql = "UPDATE CLIENTE SET NOME = @NOME, ENDERECO = @ENDERECO" +
" WHERE CODCLIENTE = @CODCLIENTE";
MySqlCommand objcmd = null;
if (this.conectar()){
try{
objcmd = new MySqlCommand(vsql, objCon); // Cria comando do MySql
// Adiciona o arrayList nos parâmetros
objcmd.Parameters.Add(new MySqlParameter("@CODCLIENTE", reg[0]));
objcmd.Parameters.Add(new MySqlParameter("@NOME", reg[1]));
objcmd.Parameters.Add(new MySqlParameter("@ENDERECO", reg[2]));
objcmd.ExecuteNonQuery(); // Executa a consulta
return true;
}
catch (MySqlException sqlerr){
MessageBox.Show(sqlerr, ":: Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
throw sqlerr;
}
finally{
this.desconectar();
}
}
else return false;
}
I hope I can help. I use this same method for Sql Server and Access with the appropriate changes.
Thank you very much for your help, gentlemen!!! I redid the entire connection, and now I noticed that it was the name of the database that was incorrect, in the host the url they generate with server uses the name of the bank at the beginning, but the name of my bank had underscore and it was replaced by normal trace, how I copied the database name of the url generated by them was giving this error!
– girotto
Dude, just to warn you, if this print info is real, take it out of the post....
– Intruso