[c#, WinForms] Etablir une connexion à une BDD sqlserver

ludo27600 Messages postés 18 Date d'inscription mercredi 5 juillet 2006 Statut Membre Dernière intervention 21 mai 2007 - 21 mai 2007 à 09:01
ludo27600 Messages postés 18 Date d'inscription mercredi 5 juillet 2006 Statut Membre Dernière intervention 21 mai 2007 - 21 mai 2007 à 16:09
bonjour,
j'essaie d'adapter la connexion à une base sql server à partir du code WebForms suivant :



void Connect(string CnxString, string MyCnx)
    {
        SqlConnection myConnection = new SqlConnection();
        string Conn = CnxString + ";User Id=" + nom.Text + ";Password=" + mdp.Text + ";";
        myConnection.ConnectionString=Conn;



        try { myConnection.Open(); }
        catch (Exception E) { Erreur("Connect", "Erreur.aspx", "", ""); }
       
        Session[MyCnx] = myConnection;
    } En WinForms :





SqlConnection connect(string MyCatalog)
        {
            System.Data.SqlClient.SqlConnection myDistantConnection = new System.Data.SqlClient.SqlConnection();
            string CnxString = "Data Source=ABC\\DEF;Initial Catalog=" + MyCatalog;



            string Conn = CnxString + ";User Id=TOTO;Password=abcdef;";



            myDistantConnection.ConnectionString = Conn;



            try { myDistantConnection.Open(); }
            catch (Exception E) { MessageBox.Show("La connexion à la base " + MyCatalog + " n'a pas pu être établie. " + E.Message.Replace('\n',' ')); }



            return (myDistantConnection);
          
        }


A chaque fois l'exception est levée sur myDistantConnection.Open(); sur Echec de la demande d'autorisation. Pourtant j'utilise le même compte qu'en WebForm.
J'ai tenté avec un nom de base factice et j'obtiens le même message d'erreur.


Je pense que la chaîne de connexion ne change pas entre WebForms et WinForms ?


Quelqu'un aurait-il une idée ?


Il y a aussi l'objet Session que je ne trouve pas en WinForms et qui est bien pratique ...


Merci.


;)

2 réponses

Tracid83 Messages postés 32 Date d'inscription jeudi 16 février 2006 Statut Membre Dernière intervention 23 mai 2007
21 mai 2007 à 15:54
Salut voici command je me connect à partir de ma winform :

string MyConnection = "database=???;server=????;User ID=???;pwd=????";
SqlConnection Connection = new SqlConnection(MyConnection);
SqlCommand myCommand = new SqlCommand(Requette);
 myCommand.Connection = Connection;
Connection.Open();

En Espérant que cela puisse t'aider.

Tracid
0
ludo27600 Messages postés 18 Date d'inscription mercredi 5 juillet 2006 Statut Membre Dernière intervention 21 mai 2007
21 mai 2007 à 16:09
Merci Tracid83.

J'ai réussi à me connecter avec :

SqlConnection connect(string MyCatalog)
{
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection();
string CnxString = "Data Source=MONSERVER\\MABASE;Initial Catalog=" + MyCatalog +
";User Id=MONUSER;Password=SONMOTDEPASSE;";

myConnection.ConnectionString = CnxString;

try { myConnection.Open(); }
catch (Exception E) { Erreur("La connexion à la base " + MyCatalog + " n'a pas pu être établie. ", E.Message, "", ""); }

return (myConnection);
}
0
Rejoignez-nous