For deploying database content, we use database script files (TSQL *.sql). When the structure of the database changes, these script must be adjusted. To change the script I use a NUnit test, that looks like:

            StringBuilder result = new StringBuilder(string.Empty);

            // Read the contents of the file
            string fileContents = File.ReadAllText(@"..\..\..\MyDatabaseProject\Contents\Customer.sql");

            // Split in lines
            string[] lines = fileContents.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            // Loop lines
            foreach (string line in lines)
            {
                // Determine if line starts with
                if (line.StartsWith("insert into Customer "))
                {
                    // Adjust line
                    string adjustedLine = line.Insert(76, "'");
                    adjustedLine = adjustedLine.Insert(81, "AA'");
                    adjustedLine = adjustedLine.Insert(85, "'");
                    adjustedLine = adjustedLine.Insert(90, "ZZ'");

                    // Add adjusted line to output
                    result.AppendLine(adjustedLine);
                }
                else
                {
                    // Add line to output
                    result.AppendLine(line);
                }
            }

            Console.Write(result.ToString());

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.