Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Data;
- using System.Globalization;
- using System.Text;
- namespace Common.Extensions
- {
- public static class DataSchemaHelper
- {
- /// <summary>
- /// Gets the DataReader's schema table fields
- /// </summary>
- public static string WriteSchemaFields(this IDataReader reader)
- {
- var dt = reader.GetSchemaTable();
- return dt.WriteSchemaFields();
- }
- /// <summary>
- /// Gets the DataTable's schema table fields from the DataReader.GetSchemaTable() method
- /// </summary>
- public static string WriteSchemaFields(this DataTable dt)
- {
- if (!dt.Columns.Contains("ColumnName") || !dt.Columns.Contains("DataType"))
- throw new ArgumentOutOfRangeException("The DataTable must contain the 'ColumnName' and 'DataType' columns. Use the DataReader.GetSchemaTable() method");
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < dt.Rows.Count; i++)
- //sb.AppendFormat("{0}[{1}]({2})", dt.Rows[i]["ColumnName"].ToString(), dt.Rows[i]["DataType"].ToString(), dt.Rows[i]["ColumnSize"].ToString())
- sb.AppendFormat("{0} {1} {2}",
- dt.Rows[i]["DataType"].ToString().Replace("System.", "").ToLower(),
- dt.Rows[i]["ColumnName"].ToString().FormatPropertyName(),
- dt.Rows[i]["ColumnName"].ToString())
- .AppendLine();
- string output = sb.ToString();
- return output;
- }
- static string FormatPropertyName(this string text)
- {
- TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
- return myTI.ToTitleCase(text.ToLower()).Replace("_", "");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment