Advertisement
Guest User

Untitled

a guest
Dec 5th, 2017
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.53 KB | None | 0 0
  1. public static class SqlHelper
  2.     {
  3.         #region Public Methods
  4.  
  5.         /// <summary>
  6.         /// Generates an insert statement for a data table
  7.         /// </summary>
  8.         /// <param name="table">The table.</param>
  9.         /// <param name="removeFields">a list of fields to be left out of the insert statement</param>
  10.         /// <returns></returns>
  11.         public static string GenerateInsert(DataTable table, string[] removeFields, string fieldToReplace, string replacementValue)
  12.         {
  13.             if (table == null)
  14.             {
  15.                 throw new ArgumentNullException("table");
  16.             }
  17.             if (string.IsNullOrEmpty(table.TableName) || table.TableName.Trim() == "")
  18.             {
  19.                 throw new ArgumentException("tablename must be set on table");
  20.             }
  21.  
  22.             var excludeNames = new SortedList<string, string>();
  23.             if (removeFields != null)
  24.             {
  25.                 foreach (string removeField in removeFields)
  26.                 {
  27.                     excludeNames.Add(removeField.ToUpper(), removeField.ToUpper());
  28.                 }
  29.             }
  30.  
  31.             var names = new List<string>();
  32.             foreach (DataColumn col in table.Columns)
  33.             {
  34.                 if (!excludeNames.ContainsKey(col.ColumnName.ToUpper()))
  35.                 {
  36.                     names.Add("[" + col.ColumnName + "]");
  37.                 }
  38.             }
  39.  
  40.             var output = new StringBuilder();
  41.  
  42.             output.AppendFormat("INSERT INTO [{0}]\n\t({1})\nVALUES ", table.TableName, string.Join(", ", names.ToArray()));
  43.  
  44.             bool firstRow = true;
  45.             foreach (DataRow rw in table.Rows)
  46.             {
  47.  
  48.                 if (firstRow)
  49.                 {
  50.                     firstRow = false;
  51.                     output.AppendLine("");
  52.                 }
  53.                 else
  54.                 {
  55.                     // there was a previous item, so add a comma
  56.                     output.AppendLine(",");
  57.                 }
  58.  
  59.                 output.Append("\t(");
  60.  
  61.                 output.Append(GetInsertColumnValues(table, rw, excludeNames, fieldToReplace, replacementValue));
  62.  
  63.                 output.Append(")");
  64.  
  65.             }
  66.  
  67.             output.Append(";");
  68.  
  69.             return output.ToString();
  70.         }
  71.  
  72.         /// <summary>
  73.         /// Little access hack
  74.         /// </summary>
  75.         /// <param name="table"></param>
  76.         /// <param name="removeFields"></param>
  77.         /// <param name="fieldToReplace"></param>
  78.         /// <param name="replacementValue"></param>
  79.         /// <returns></returns>
  80.         public static string[] GenerateInserts(DataTable table, string[] removeFields, string fieldToReplace, string replacementValue)
  81.         {
  82.             if (table == null)
  83.             {
  84.                 throw new ArgumentNullException("table");
  85.             }
  86.             if (string.IsNullOrEmpty(table.TableName) || table.TableName.Trim() == "")
  87.             {
  88.                 throw new ArgumentException("tablename must be set on table");
  89.             }
  90.  
  91.             var excludeNames = new SortedList<string, string>();
  92.             if (removeFields != null)
  93.             {
  94.                 foreach (string removeField in removeFields)
  95.                 {
  96.                     excludeNames.Add(removeField.ToUpper(), removeField.ToUpper());
  97.                 }
  98.             }
  99.  
  100.             var names = new List<string>();
  101.             foreach (DataColumn col in table.Columns)
  102.             {
  103.                 if (!excludeNames.ContainsKey(col.ColumnName.ToUpper()))
  104.                 {
  105.                     names.Add("[" + col.ColumnName + "]");
  106.                 }
  107.             }
  108.  
  109.             List<string> inserts = new List<string>();
  110.  
  111.             var output = new StringBuilder();
  112.  
  113.  
  114.             foreach (DataRow rw in table.Rows)
  115.             {
  116.                 output = new StringBuilder();
  117.  
  118.                 output.AppendFormat("INSERT INTO [{0}]\n\t({1})\nVALUES ", table.TableName, string.Join(", ", names.ToArray()));
  119.  
  120.                 bool firstRow = true;
  121.                 if (firstRow)
  122.                 {
  123.                     output.AppendLine("");
  124.                 }
  125.  
  126.                 output.Append("\t(");
  127.  
  128.                 output.Append(GetInsertColumnValues(table, rw, excludeNames, fieldToReplace, replacementValue));
  129.  
  130.                 output.Append(")");
  131.  
  132.                 output.Append(";");
  133.  
  134.                 inserts.Add(output.ToString());
  135.             }
  136.  
  137.             return inserts.ToArray();
  138.         }
  139.  
  140.  
  141.  
  142.         /// <summary>
  143.         /// Gets the column values list for an insert statement
  144.         /// </summary>
  145.         /// <param name="table">The table</param>
  146.         /// <param name="row">a data row</param>
  147.         /// <param name="excludeNames">A list of fields to be excluded</param>
  148.         /// <returns></returns>
  149.         public static string GetInsertColumnValues(DataTable table, DataRow row, SortedList<string, string> excludeNames, string fieldToReplace, string replacementValue)
  150.         {
  151.             var output = new StringBuilder();
  152.  
  153.             bool firstColumn = true;
  154.  
  155.             foreach (DataColumn col in table.Columns)
  156.             {
  157.                 if (!excludeNames.ContainsKey(col.ColumnName.ToUpper()))
  158.                 {
  159.                     if (firstColumn)
  160.                     {
  161.                         firstColumn = false;
  162.                     }
  163.                     else
  164.                     {
  165.                         output.Append(", ");
  166.                     }
  167.  
  168.                     if (fieldToReplace != null && col.ColumnName.Equals(fieldToReplace, StringComparison.InvariantCultureIgnoreCase))
  169.                     {
  170.                         if (replacementValue == null)
  171.                         {
  172.                             output.Append("NULL");
  173.                         }
  174.                         else
  175.                         {
  176.                             output.Append(replacementValue);
  177.                         }
  178.                     }
  179.                     else
  180.                     {
  181.                         output.Append(GetInsertColumnValue(row, col));
  182.                     }
  183.                 }
  184.             }
  185.  
  186.             return output.ToString();
  187.         }
  188.  
  189.         /// <summary>
  190.         /// Gets the insert column value, adding quotes and handling special formats
  191.         /// </summary>
  192.         /// <param name="row">The row.</param>
  193.         /// <param name="column">The column</param>
  194.         /// <returns></returns>
  195.         public static string GetInsertColumnValue(DataRow row, DataColumn column)
  196.         {
  197.             string output = "";
  198.  
  199.             if (row[column.ColumnName] == DBNull.Value)
  200.             {
  201.                 output = "NULL";
  202.             }
  203.             else
  204.             {
  205.                 if (column.DataType == typeof(bool))
  206.                 {
  207.                     output = (bool)row[column.ColumnName] ? "1" : "0";
  208.                 }
  209.                 else
  210.                 {
  211.                     bool addQuotes = false;
  212.                     addQuotes = addQuotes || (column.DataType == typeof(string));
  213.                     addQuotes = addQuotes || (column.DataType == typeof(DateTime));
  214.  
  215.                     if (addQuotes)
  216.                     {
  217.                         output = "'" + row[column.ColumnName].ToString() + "'";
  218.                     }
  219.                     else
  220.                     {
  221.                         output = row[column.ColumnName].ToString();
  222.                     }
  223.                 }
  224.             }
  225.  
  226.             return output;
  227.         }
  228.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement