2009-08-13 65 views
0

我有一個與我的MysqlConnection objets小問題。我有這兩個連接字符串在我的app.config:MysqlCommand切換連接意外

<connectionStrings> 

    <add name="bdpvcLocalhost" connectionString="Persist Security Info=False;server=localhost;Uid=root;Password=***;database=bdpvc" providerName="Mysql.Data.MySqlClient"/> 
    <add name="bdpvcProduction" connectionString="server=pvcserver.pvcservprod.local;user id=pvc_app;Pwd=***;database=bdpvc" providerName="Mysql.Data.MySqlClient"/> 
</connectionStrings> 

第一個連接是本地測試數據庫,第二個是真正的生產數據庫。由於我在開發早期,我使用本地數據庫。然後我用下面的代碼(我切一些參數定義事業真的是有很多)

using (MySqlConnection connection = new MySqlConnection(_connectionString)) 
{ 

    connection.Open(); 

    using (MySqlTransaction transaction = connection.BeginTransaction()) 
    { 
     const string sequenceQuery = "INSERT INTO sequence " + 
            "(No_Sequence, No_Client, No_Produit, No_Version_Produit, " + 
            " No_Soumission, No_Commande, No_Reference, Date_Livraison, " + 
            "Date_Commande, Date_Soumission, Quantite, Status, Contact, " + 
            "Notes, No_Production_Ancien, Erreur_Production, No_Fichier_Log, " + 
            "No_Utilisateur, Date_Enregistrement, Extension_Fichier_Fax, Uuid) " + 

            "VALUES(?No_Sequence, ?No_Client, ?No_Produit, ?No_Version_Produit, " + 
            "?No_Soumission, ?No_Commande, ?No_Reference, ?Date_Livraison, ?Date_Commande, " + 
            "?Date_Soumission, ?Quantite, ?Status, ?Contact, ?Notes, ?No_Production_Ancien, " + 
            "?Erreur_Production, ?No_Fichier_Log, ?No_Utilisateur, ?Date_Enregistrement, " + 
            "?Extension_Fichier_Fax, ?Uuid)"; 

     const string selectSequenceNoQuery = 
      "SELECT MAX(No_Sequence) AS Valeur_No_Increment FROM sequence"; 

     const string updateSequenceNoQuery = 
      "UPDATE tables SET Valeur_No_Increment = ?Valeur_No_Increment WHERE Nom_Tables = 'sequence'"; 

     int currentIncrement = 0; 


     MySqlCommand selectNoSequenceCommand = new MySqlCommand(selectSequenceNoQuery, connection, 
                   transaction); 
     MySqlCommand updateNoSequenceCommand = new MySqlCommand(updateSequenceNoQuery, connection, 
                   transaction); 
     MySqlCommand insertSequenceCommand = new MySqlCommand(sequenceQuery, connection, transaction); 

     //------------------------------ 
     //This query execute perfectly! 
     currentIncrement = Int32.Parse(selectNoSequenceCommand.ExecuteScalar().ToString()); 


     insertSequenceCommand.Parameters.Add("?No_Sequence", MySqlDbType.String); 
     //Lots and lot of parameters definition 

     foreach (Sequence sequence in _sequences) 
     { 
      currentIncrement++; 

      sequence.No_Sequence = currentIncrement.ToString(); 


      insertSequenceCommand.Parameters["?No_Sequence"].Value = sequence.No_Sequence; 
      //Lots and lots of value assignement 

      //--------------------------------- 
      //But this one doesn't use the right user! 
      insertSequenceCommand.ExecuteNonQuery(); 

      Console.WriteLine("Inserted new sequence with Uuid " + sequence.Uuid + " and increment " + 
          sequence.No_Sequence); 

     } 

     updateNoSequenceCommand.Parameters.AddWithValue("?Valeur_No_Increment", currentIncrement); 
     updateNoSequenceCommand.ExecuteNonQuery(); 

     transaction.Commit(); 
    } 
} 

第一個查詢執行就好了。但是,第二個查詢無法執行,因爲「用戶pvc_app不存在」。 但我以root身份連接到本地數據庫!而且從來沒有在我的代碼中切換連接字符串!我試圖刪除app.config中的第二個連接字符串,但它一直試圖連接爲pvc_app。我多次重建應用程序,重新啓動計算機,甚至卸載/重新安裝了ADO.NET連接器,但它一直試圖以pvc_app連接!我在這裏做錯了什麼?

現在,這個神祕的「連接字符串緩存」位於哪裏,所以我可以用我的裸手殺死它?因爲現在真的讓我的生活變得不幸。

回答

0

解決此問題的方法是使用默認密碼創建另一個用戶。連接然後工作正常。這似乎是ADO.NET連接器的一個缺陷。