[c#, WinForms]Problème de connexion à base de données
bonjour,
j'essaie d'adapter la connexion à une base sql server à partir du code WebForms suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11
| 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 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 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
Code:
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.
;)