0

OK here my piece of code

MySqlConnection conn = new MySqlConnection("Userid=root;pwd=root;port=3306;host=localhost;database=test");
conn.Open();

Due to some issue with the new version of the devart connector i'm using i have to add a line of code OldCompatibility.BinaryAsString = true; everywhere in my code as shown below

OldCompatibility.BinaryAsString = true;
MySqlConnection conn = new MySqlConnectio("User id=root;pwd=root;port=3306;host=localhost;database=test");
conn.Open();

But the problem is i have to make this change all over my application which have many pages with this piece of code.So is there any way to do this globally so that i dont have to make this change all over my application. i'm using devart connector 6

Jay
  • 373
  • 8
  • 22
  • For future, you would want to write one function for creating connection and centralizing the code :). For now : Do you have any base class or anything which is common to all pages ? You may want to put this line in a function which is called by every page. – Beenish Khan Apr 17 '12 at 04:31

5 Answers5

4

Assuming that your connection is remotely similar each time; don't. Keep all your connection logic in one shared method, and use that instead of repeating the connection code everywhere.

public static MySqlConnection Connect() {
    OldCompatibility.BinaryAsString = true;
    MySqlConnection conn = new MySqlConnection("User id=root;pwd=root;port=3306;host=localhost;database=test");
    conn.Open();

    return conn;
}
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • well in my application i'm not using any custom method like Connect so its not possible to do dat way coz if i do den i would have to change it everywhere – Jay Apr 17 '12 at 04:34
  • 1
    @Jay: That's the point. You'll pretty much have to change it everywhere regardless - you can use Find and Replace to help you with that, and that's about it. But if/when you have to change it again, you *won't* have to change it everywhere. Just use a method, stay [DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself "Don't repeat yourself"). – Ry- Apr 17 '12 at 04:36
1

MySqlConnection is your custom class if you put OldCompatibility.BinaryAsString = true in the constructor of MySqlConnection then it would work

Adil
  • 146,340
  • 25
  • 209
  • 204
0

You can use a factory method

public class FactoryMethods
{
  public static MySqlConnection GetConfiguredConnection()
  {
    OldCompatibility.BinaryAsString = true;
    MySqlConnection conn = new MySqlConnectio("User id=root;pwd=root;port=3306;host=localhost;database=test");
    conn.Open();
    return conn;
  }
}

And assuming the connection is IDispose-able

using (var myConn = FactoryMethods.GetConfiguredConnection())
{
  // Use your connection here
}
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • 1
    Though this is the right way to do it but again, he would need to make the change everywhere. If poster can live with that then I second this answer too. – Beenish Khan Apr 17 '12 at 04:33
  • @nonnb cant do it this way since that would require me to make changes all over my project – Jay Apr 17 '12 at 04:36
  • Doesn't DevArt have a web.config setting where you can globally tweak the compatability option? In future would recommend that you encapsulate and / or inject the creation of complex objects like SQL Connection strings to prevent this kind of maintenance issue. – StuartLC Apr 17 '12 at 04:41
0

Change all your code at once with find and replace using regular expressions:

1.- Hit Ctrl+H to open the “Find and Replace” window Enter your search, replace line breaks with “\n” (no quotes)

2.- Expand “Find options” and check “Use”, select “Regular Expressions” (this also activates the right arrow next to the “Find what” box. It lists a few commands/shortcuts).

For example: Type this on find:

{MySqlConnection conn = new MySqlConnection}

Then try this on replace:

OldCompatibility.BinaryAsString = true;\n\t\t\1 MySqlConnection conn = new MySqlConnection

This will find all the code that matches the search and add an additional line infront of it. Notice the place holder {...} being replaced with \1

Ulises
  • 13,229
  • 5
  • 34
  • 50
0

You should really consider @minitech's suggestion on using a method, but if you really can't change the code anywhere, there are only really two options I can see;

  • Extend MySQLConnection to a new class using the same name in another namespace, just changing the constructor to include your line. Then replace only the using line at the top of the file.

  • If that can't be done, implement a wrapper class in the same manner.

Both these options may fail to work (#1 if the class is final for example, and #2 if the class is passed as a parameter to any functions), but unless you can at least do a global search/replace or implement @minitech's suggestion with a method, I can't see any other way.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294