Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class SqlHelper
- {
- #region Public Methods
- /// <summary>
- /// Generates an insert statement for a data table
- /// </summary>
- /// <param name="table">The table.</param>
- /// <param name="removeFields">a list of fields to be left out of the insert statement</param>
- /// <returns></returns>
- public static string GenerateInsert(DataTable table, string[] removeFields, string fieldToReplace, string replacementValue)
- {
- if (table == null)
- {
- throw new ArgumentNullException("table");
- }
- if (string.IsNullOrEmpty(table.TableName) || table.TableName.Trim() == "")
- {
- throw new ArgumentException("tablename must be set on table");
- }
- var excludeNames = new SortedList<string, string>();
- if (removeFields != null)
- {
- foreach (string removeField in removeFields)
- {
- excludeNames.Add(removeField.ToUpper(), removeField.ToUpper());
- }
- }
- var names = new List<string>();
- foreach (DataColumn col in table.Columns)
- {
- if (!excludeNames.ContainsKey(col.ColumnName.ToUpper()))
- {
- names.Add("[" + col.ColumnName + "]");
- }
- }
- var output = new StringBuilder();
- output.AppendFormat("INSERT INTO [{0}]\n\t({1})\nVALUES ", table.TableName, string.Join(", ", names.ToArray()));
- bool firstRow = true;
- foreach (DataRow rw in table.Rows)
- {
- if (firstRow)
- {
- firstRow = false;
- output.AppendLine("");
- }
- else
- {
- // there was a previous item, so add a comma
- output.AppendLine(",");
- }
- output.Append("\t(");
- output.Append(GetInsertColumnValues(table, rw, excludeNames, fieldToReplace, replacementValue));
- output.Append(")");
- }
- output.Append(";");
- return output.ToString();
- }
- /// <summary>
- /// Little access hack
- /// </summary>
- /// <param name="table"></param>
- /// <param name="removeFields"></param>
- /// <param name="fieldToReplace"></param>
- /// <param name="replacementValue"></param>
- /// <returns></returns>
- public static string[] GenerateInserts(DataTable table, string[] removeFields, string fieldToReplace, string replacementValue)
- {
- if (table == null)
- {
- throw new ArgumentNullException("table");
- }
- if (string.IsNullOrEmpty(table.TableName) || table.TableName.Trim() == "")
- {
- throw new ArgumentException("tablename must be set on table");
- }
- var excludeNames = new SortedList<string, string>();
- if (removeFields != null)
- {
- foreach (string removeField in removeFields)
- {
- excludeNames.Add(removeField.ToUpper(), removeField.ToUpper());
- }
- }
- var names = new List<string>();
- foreach (DataColumn col in table.Columns)
- {
- if (!excludeNames.ContainsKey(col.ColumnName.ToUpper()))
- {
- names.Add("[" + col.ColumnName + "]");
- }
- }
- List<string> inserts = new List<string>();
- var output = new StringBuilder();
- foreach (DataRow rw in table.Rows)
- {
- output = new StringBuilder();
- output.AppendFormat("INSERT INTO [{0}]\n\t({1})\nVALUES ", table.TableName, string.Join(", ", names.ToArray()));
- bool firstRow = true;
- if (firstRow)
- {
- output.AppendLine("");
- }
- output.Append("\t(");
- output.Append(GetInsertColumnValues(table, rw, excludeNames, fieldToReplace, replacementValue));
- output.Append(")");
- output.Append(";");
- inserts.Add(output.ToString());
- }
- return inserts.ToArray();
- }
- /// <summary>
- /// Gets the column values list for an insert statement
- /// </summary>
- /// <param name="table">The table</param>
- /// <param name="row">a data row</param>
- /// <param name="excludeNames">A list of fields to be excluded</param>
- /// <returns></returns>
- public static string GetInsertColumnValues(DataTable table, DataRow row, SortedList<string, string> excludeNames, string fieldToReplace, string replacementValue)
- {
- var output = new StringBuilder();
- bool firstColumn = true;
- foreach (DataColumn col in table.Columns)
- {
- if (!excludeNames.ContainsKey(col.ColumnName.ToUpper()))
- {
- if (firstColumn)
- {
- firstColumn = false;
- }
- else
- {
- output.Append(", ");
- }
- if (fieldToReplace != null && col.ColumnName.Equals(fieldToReplace, StringComparison.InvariantCultureIgnoreCase))
- {
- if (replacementValue == null)
- {
- output.Append("NULL");
- }
- else
- {
- output.Append(replacementValue);
- }
- }
- else
- {
- output.Append(GetInsertColumnValue(row, col));
- }
- }
- }
- return output.ToString();
- }
- /// <summary>
- /// Gets the insert column value, adding quotes and handling special formats
- /// </summary>
- /// <param name="row">The row.</param>
- /// <param name="column">The column</param>
- /// <returns></returns>
- public static string GetInsertColumnValue(DataRow row, DataColumn column)
- {
- string output = "";
- if (row[column.ColumnName] == DBNull.Value)
- {
- output = "NULL";
- }
- else
- {
- if (column.DataType == typeof(bool))
- {
- output = (bool)row[column.ColumnName] ? "1" : "0";
- }
- else
- {
- bool addQuotes = false;
- addQuotes = addQuotes || (column.DataType == typeof(string));
- addQuotes = addQuotes || (column.DataType == typeof(DateTime));
- if (addQuotes)
- {
- output = "'" + row[column.ColumnName].ToString() + "'";
- }
- else
- {
- output = row[column.ColumnName].ToString();
- }
- }
- }
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement