MysqlCommand意外切换连接

时间:2009-08-13 01:20:46

标签: mysql ado.net

我的MysqlConnection对象有点问题。我的app.config中有两个连接字符串:

<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!我在这里做错了吗?

现在,这个神秘的“连接字符串缓存”位于何处,所以我可以用我的裸手杀死它?因为它现在真的让我的生活变得痛苦。

1 个答案:

答案 0 :(得分:0)

此问题的解决方法是使用默认密码创建另一个用户。然后连接工作正常。它似乎是ADO.NET连接器的一个错误。