sql server connexion

n0uki

New member
Joined
May 2, 2021
Messages
2
Programming Experience
Beginner
Hello Developer Community,

I'm trying to create a little app for my job. But it's the first time I try to program...

For now, i'm here :

1619962888542.png


I want to connect to sql server when click on button "se connecter". After that, if connexion is OK , i would like write at the right button "connexion OK".

Someone can help me ?
 
Yes we can help you, but we are not a code writing service. You have to show some effort by telling us what you have already considered and thought about, as well as showing us your current code. Tell us where you are stuck and we can try to guide you.

Is this UI going to be with WinForms, WPF, or some other framework? This is so that we can move this thread to a more appropriate forum instead of the "C# General Discussion" because some of the techniques for swapping control UI is framework specific.
 
Hello,

so my project :
1619969755452.png


I think a windows form.

And for the code, i'm here.

C#:
using System;
using System.Windows.Forms;
using Microsoft.Data.SqlClient;
using System.Data.SqlClient;

namespace DeconnexionUserSage
{
    public partial class StopUserSage : Form
    {
        public StopUserSage()
        {
            InitializeComponent();
        }

        private void StopUserSage_Load(object sender, EventArgs e)
        {
            authentification_combobox.SelectedIndex = 0;
            sqluser_textbox.Enabled = false;
            sqlpassword_textbox.Enabled = false;
        }

        private void Authentification_combobox_SelectedIndexChanged(object sender, EventArgs e)
        {
        
            if (authentification_combobox.SelectedIndex == 0)
            {
                sqluser_textbox.Enabled = false;
                sqluser_textbox.Clear();
                sqlpassword_textbox.Enabled = false;
                sqlpassword_textbox.Clear();
            }
                      
            if (authentification_combobox.SelectedIndex == 1)
            {
                sqluser_textbox.Enabled = true;
                sqlpassword_textbox.Enabled = true;
            }             
        }

        private void Connexion_button_Click(object sender, EventArgs e)
        {
            ///
            /// Affichage d'un message d'erreur si le nom du serveur n'est pas renseigné
            ///
            if (ServerName_textbox.Text.Trim() == "")
            {
                MessageBox.Show("Vous devez renseigner un nom de serveur", "Paramètres de connexion",MessageBoxButtons.OK,MessageBoxIcon.Error);

            }

            if (authentification_combobox.SelectedIndex == 1)
            {
                if (sqluser_textbox.Text.Trim() == "" || sqlpassword_textbox.Text.Trim() == "")
                {
                    MessageBox.Show("L'utilisateur ou le mot de passe est incorrect", "sql.server.auth", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}

Now, i search the way to test the SQL server connexion. I have look a possibility with SqlConnectionStringBuilder from System.Data.SqlClient...
 
Last edited:
There appears to be no data access code in there at all, so you need to do some research on data access in C#. Sites like this one should be used to help with specific problems, not to have people teach you the basics. You should do some research on ADO.NET in general and using the SqlClient provider for SQL Server in particular and then try to do whatever it is that you want to do. If what you do doesn't work, then you should ask for help with that specifically. I'm not trying to discourage you posting here on this site but you should always be looking for existing information first and using that, then posting here when you hit a dead end. If you haven't searched the web for "C# connect to SQL Server" or the like and used all the relevant information you can find there then it's really too soon to be posting a question here.
 
Back
Top Bottom