2

I would like to know if it's possible to ignore a file or a part of my code during a commit. To develop my solution I actually use two different computers, each using his own connection string to the database.

    //Informations - DB
    private const String _SERVER = "localhost";
    private const String _DATABASE = "dbname";
    private const String _UID = "";
    private const String _PASSWORD = "";

    //DB connection
    public static MySqlConnection v_DBConnection;

    public static void InititializeDB()
    {
        String v_ConnectionString = null;
        MySqlConnectionStringBuilder v_MySqlConnectionStringBuilder;

        try
        {
            v_ConnectionString = null;
            v_MySqlConnectionStringBuilder = new MySqlConnectionStringBuilder();

            v_MySqlConnectionStringBuilder.Server = _SERVER;
            v_MySqlConnectionStringBuilder.UserID = _UID;
            v_MySqlConnectionStringBuilder.Password = _PASSWORD;
            v_MySqlConnectionStringBuilder.Database = _DATABASE;

            //CONNECTION CHAIN
            v_ConnectionString = v_MySqlConnectionStringBuilder.ToString();

            v_DBConnection = new MySqlConnection(v_ConnectionString);

        }
        catch (Exception exception)
        {
            MessageBox.Show("InititializeDB fail due to:" + exception.ToString());
        }
    }

I would like during a commit ignore the file containing the string or exclude a part of the code (connection string).

I tried to add the file containing my connection chain in my .gitignore file but I'm not sure, I did'nt understandhow the .gitignore file work.

# Ignored files
DBConnection.cs
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
InFiniTy
  • 33
  • 8
  • As it should... – Patrick Hofman Jan 02 '18 at 10:35
  • 1
    Are you asking because you're not sure or because it doesn't work? If it doesn't work, what exactly shows that? – lucidbrot Jan 02 '18 at 10:36
  • 1
    If I am not mistaken, GIT if a file based source control, so no way to ignore part of a file. There are several way you could make it. You could use a App.config file and store the connectionstring in it. You could split your class into two files declaring it a partial class (same class in two diferent files, one of then referenced in the .gitignore). If you dont want the user to get access to the connectionstring I would suggest the partial classes. – Cleptus Jan 02 '18 at 10:37
  • lucidbrot : (it doesn't work) - Actually when I modify my "DBConnection.cs" file. when I make a git status, add, commit ... the file is always push. bradbury9 : I think that is a good solution (2 différent partial class) thank you ! But when i reference my file in my .gitinogne the file is constantly push. – InFiniTy Jan 02 '18 at 10:46
  • 1
    Check this answer, seems related https://stackoverflow.com/a/1139797/2265446 – Cleptus Jan 02 '18 at 10:50
  • Ho ! Thanks bradbury9, that's worked ! – InFiniTy Jan 02 '18 at 10:59
  • That's works well :) – InFiniTy Jan 02 '18 at 13:13
  • You can't "ignore" part of a file, but you can only commit part by using `git add --patch`. – evolutionxbox Jan 02 '18 at 14:03
  • evolutionxbox i note it thank you :) – InFiniTy Jan 02 '18 at 15:06

0 Answers0