Connect MySql C #

1

I have a MYSQL database on a shared hosting UOLHOST, I can connect to the database through the Visual Studio Server Explorer normally:

HoweverwhentryingtoconnectthroughasimpletestconsoleapplicationIgetaccessdenied:

The clear difference I noticed was that apparently mysql adds an IP to the user, in the connection via server explorer the user gets

  

like this: [email protected]

and in the connection via console the user looks like this:

  

'barc' @ '191.17.28.80'

The error I get is this:

  

Authentication to host 'barcelona-app.mysql.uhserver.com' for user   'barc' using method 'mysql_native_password' failed with message:   Access denied for user 'barc'@'191.17.28.80' (using password: YES

Can anyone help me?

    
asked by anonymous 02.08.2016 / 02:40

3 answers

1

Hello.

  • First you need to download the Connector / Net (since you are using ADO )
  • Import% of%
  • Soon you can do something like this
  • See the Code below:

    //define o dataset
    mDataSet = new DataSet();
    
    //define string de conexao e cria a conexao
    mConn = new MySqlConnection("Persist Security Info=False;server=localhost;database=Cadastro;uid=root;server=localhost;database=Cadastro;uid=root;pwd=xxxx");
    
      try{
        //abre a conexao
         mConn.Open();
       }
       catch(System.Exception e)
       {
        MessageBox.Show(e.Message.ToString());
       }
    
       //verificva se a conexão esta aberta
      if (mConn.State == ConnectionState.Open)
      {
        //cria um adapter usando a instrução SQL para acessar a tabela Clientes
        mAdapter = new  MySqlDataAdapter("SELECT * FROM Clientes", mConn);
        //preenche o dataset via adapter
        mAdapter.Fill(mDataSet, "Clientes");
        //atribui a resultado a propriedade DataSource do DataGrid
        mDataGrid.DataSource = mDataSet;
        mDataGrid.DataMember = "Clientes";
    }
    

    See the full step by step here

        
    02.08.2016 / 15:52
    0

    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 queries:

    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;
        }
    

    Hope you can help. I use this same method for Sql Server and Access with the appropriate changes.

        
    02.08.2016 / 10:42
    -1

    Greetings!

    Friend, I believe there are several ways to make that connection. Here's one I used on my college project last semester.

            string Source = "aonde o banco esta";
            string Banco = "qual banco";
            string User = "nome do usuario";
            string Senha = "senha do usuario";
    
            SqlConnection NewConexao = new SqlConnection("Data Source="+ Source 
                +"; Initial Catalog="+ Banco 
                +"; User Id="+User
                +"; Password="+Senha
                +"; Integrated Security=false");
    
            NewConexao.Open();
    

    I used this method to "open" my connection to the sqlServer database < - > C #, I believe it might suit you with mysql as well. I hope it helps.

        
    02.08.2016 / 03:22