Comment se connecter à une base de donnée hébergée ?

Bonjour,

J'aimerai créer un formulaire de connexion pour mon application C#, comment je pourrais me connecter à une base de donnée hébergée ?

Merci pour votre aide !

3 réponses

  • Votes
  • Date
  • Date décroissante
  1. Contributeur
    Hébergée, mais hébergée où?

    SI c'est un serveur mutualisé d'un des nombreux hébergeurs, en théorie non. Si c'est sûr un serveur dédié, alors tout est possible.
    1. Pour l'hébergeur il n'y a pas de problème, je sais que ça marche très bien avec. Je suis chez Xeround
  2. Salut,

    Généralement par défaut les bases hébergées ne sont pas accessibles à distance pour des raisons de sécurité.

    Il faut voir avec ton hébergeur quels sont les paramétrages et services qu'il propose dans le domaine.
    1. Mon hébergeur est bien accessibles à distance
    2. Salut,

      Ca veux rien dire "Mon hébergeur est bien accessibles à distance".
      Par défaut la base l'est pas. Tu sais accéder à ta base MySQL à distance ?
  3. Toujours aucune solutions ? :/ Je vous partage mon code, voici le code du program.cs :

    namespace WindowsFormsApplication1
    {
        static class Program
        {
            /// <summary>
            /// Point d'entrée principal de l'application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                string connectionString = null;
                try
                {
                    connectionString = ConfigurationManager.ConnectionStrings["db_gsb"].ConnectionString;
                    MessageBox.Show("configuration réussie");
                    using (MySqlConnection connexion = new MySqlConnection(connectionString))
                    {
                        connexion.Open();
                        //MySqlCommand sqlCommand = new MySqlCommand("select * from administrateur;", connexion);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    


    Voilà celui de mon fichier de configuration:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="db_gsb" connectionString="Server=_______;port=_____;Database=____;Uid=____;password=______;" />
      </connectionStrings>
    </configuration>


    Voilà celui de mon formulaire de connexion:

    namespace WindowsFormsApplication1 
    {
        public partial class Form1 : Form 
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                mdptxt.Clear();
                logintxt.Clear();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if ( conn(logintxt.Text, mdptxt.Text) == 1 )
                {
                    MessageBox.Show("Connexion réussie");
                    //ajouter lien vers l'application
                }
                else
                {
                    MessageBox.Show("Mot de passe ou login incorrect !");
                }
            }
    
            private int conn (string user, string pass) 
            {
                user.Trim();
                pass.Trim();
                string connectionString = null;
    
                try
                {
                    connectionString = ConfigurationManager.ConnectionStrings["db_gsb"].ConnectionString;
                    
                    using (MySqlConnection connexion = new MySqlConnection(connectionString))
                    {
                      connexion.Open();
                        MySqlCommand sqlCommand = new MySqlCommand("select * from administrateur where username='"+user+"' and password='"+pass+"';");
                        if (sqlCommand.ExecuteScalar() == null)
                        {
                            return 0;
                        }
                        else
                        {
                            return 1;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return 0;
                }
            }
    
            private void mdpForgot_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
            {  
               Form2 form = new Form2();
               form.Show();
               mdpForgot.LinkVisited = true;
            }
    
        }
    }