private static void ChangeColumnDataType(DataTable table, DataColumn column, Type newtype) { if (table.Columns.Contains(column.ColumnName) == false) { return; } try { var newcolumn = new DataColumn("temp", newtype); table.Columns.Add(newcolumn); foreach (DataRow row in table.Rows) { try { row["temp"] = StiConvert.ChangeType(row[column], newtype); } catch { } } var indexColumn = table.Columns.IndexOf(column); table.Columns.Remove(column); newcolumn.ColumnName = column.ColumnName; table.Columns[newcolumn.ColumnName].SetOrdinal(indexColumn); } catch (Exception) { } }
public void FillDataTable(DataTable table, string query) { if (string.IsNullOrEmpty(this.ConnectionString)) { return; } var currentCulture = Thread.CurrentThread.CurrentCulture; try { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false); using (var client = GetDefaultWebClient()) { var url = StiUrl.Combine(this.Address, query); var metadata = client.DownloadString(url); #region JSON object odata = null; try { odata = JsonConvert.DeserializeObject(metadata); } catch { } if (odata != null && odata is JObject) { JArray values = null; var jObject = odata as JObject; if (jObject != null) { foreach (var child in jObject.Children()) { var jProperty = child as JProperty; if (jProperty != null && jProperty.Name == "value" && jProperty.Value is JArray) { values = jProperty.Value as JArray; } } } if (values != null) { foreach (JObject value in values.ChildrenTokens) { var row = table.NewRow(); foreach (JProperty columnObjValue in value.ChildrenTokens) { try { var columnName = columnObjValue.Name; var columnValue = columnObjValue.Value; var currentColumn = table.Columns[columnName]; if (currentColumn != null) { var currentValue = StiConvert.ChangeType(columnValue, currentColumn.DataType); row[columnName] = currentValue ?? DBNull.Value; } } catch { } } table.Rows.Add(row); } } } #endregion #region XML else { using (var reader = new StringReader(metadata)) using (var xmlReader = XmlReader.Create(reader)) { var root = XElement.Load(xmlReader); var title = root.Elements().FirstOrDefault(e => e.Name.LocalName == "title"); if (title != null) { table.TableName = title.Value; } foreach (var entry in root.Elements().Where(e => e.Name.LocalName == "entry")) { var elementContent = entry.Elements().FirstOrDefault(e => e.Name.LocalName == "content"); if (elementContent == null) { continue; } var elementProperties = elementContent.Elements().FirstOrDefault(e => e.Name.LocalName.EndsWith("properties")); if (elementProperties == null) { continue; } var row = table.NewRow(); #region Name try { var elementTitle = entry.Elements().FirstOrDefault(e => e.Name.LocalName == "title"); if (elementTitle != null && table.Columns["Name"] != null) { row["Name"] = elementTitle.Value; } } catch { } #endregion #region Description try { var elementSummary = entry.Elements().FirstOrDefault(e => e.Name.LocalName == "summary"); if (elementSummary != null && table.Columns["Description"] != null) { row["Description"] = elementSummary.Value; } } catch { } #endregion foreach (var elementProperty in elementProperties.Elements()) { try { var columnName = elementProperty.Name.LocalName.Replace("d:", ""); var columnValue = elementProperty.Value; var currentColumn = table.Columns[columnName]; if (currentColumn != null) { var value = StiConvert.ChangeType(columnValue, currentColumn.DataType); row[columnName] = value ?? DBNull.Value; } } catch { } } table.Rows.Add(row); } } } #endregion } } catch { } finally { Thread.CurrentThread.CurrentCulture = currentCulture; } }
internal DbParameter CreateParameterWithTypeAndSize(string typeName, string parameterName, int type, int size, Type dbType) { return(CreateObject(typeName, parameterName, StiConvert.ChangeType(type, dbType), size) as DbParameter); }