/// <summary> /// 對目的資料表新增資料列,有相同欄位時才會塞值。 /// </summary> /// <param name="destDataTable">目的資料表。</param> /// <param name="sourceRow">來源資料列。</param> /// <param name="allowNull">是否允許Null值</param> public static void AddRow(DataTable destDataTable, DataRow sourceRow, bool allowNull = true) { DataRow oNewRow; string sColumnName; if (BaseFunc.IsNull(destDataTable)) { return; } if (BaseFunc.IsEmpty(sourceRow)) { return; } oNewRow = destDataTable.NewRow(); foreach (DataColumn column in destDataTable.Columns) { sColumnName = column.ColumnName; if (sourceRow.Table.Columns.Contains(sColumnName)) { var value = sourceRow[sColumnName]; if (allowNull || !BaseFunc.IsNullOrDBNull(value)) { oNewRow[sColumnName] = value; } } } destDataTable.Rows.Add(oNewRow); }
/// <summary> /// 取得欄位值,若不存在該欄位或為 DbNull 值,則傳回預設值。 /// </summary> /// <param name="row">資料列。</param> /// <param name="fieldName">欄位名稱。</param> /// <param name="defaultValue">預設值。</param> public static object GetFieldValue(DataRow row, string fieldName, object defaultValue) { object oFieldValue; // 若資料列為 Null 則傳回預設值 if (BaseFunc.IsNull(row)) { return(defaultValue); } // 若不存在該欄位,則傳回預設值 if (!HasField(row, fieldName)) { return(defaultValue); } oFieldValue = row[fieldName]; if (BaseFunc.IsNullOrDBNull(oFieldValue)) { return(defaultValue); // 若欄位值為 DbNull 值,則傳回預設值 } else { return(oFieldValue); } }
/// <summary> /// 取得資料表的異動資料。 /// </summary> /// <param name="dataTable">資料表</param> public static DataTable DataTableGetChanges(DataTable dataTable) { if (BaseFunc.IsNull(dataTable)) { return(null); } else { return(dataTable.GetChanges()); } }
/// <summary> /// 判斷資料表是否有異動。 /// </summary> /// <param name="dataTable">資料表。</param> public static bool DataTableIsChanged(DataTable dataTable) { if (BaseFunc.IsNull(DataTableGetChanges(dataTable))) { return(false); } else { return(true); } }
/// <summary> /// 取得指定名稱的整數 /// </summary> /// <param name="source"></param> /// <param name="propertyName"></param> /// <returns></returns> public static int TokenAsInt(this JObject source, string propertyName) { var value = source.TokenValue(propertyName); if (BaseFunc.IsNull(value)) { return(0); } else { return(BaseFunc.CInt(value.ToString())); } }
/// <summary> /// 取得指定名稱的Json陣列 /// </summary> /// <param name="source"></param> /// <param name="propertyName"></param> /// <returns></returns> public static JArray TokenAsJArray(this JObject source, string propertyName) { var value = source.TokenValue(propertyName); if (BaseFunc.IsNull(value)) { return(null); } else { return(value as JArray); } }
/// <summary> /// 取得指定名稱的布林值 /// </summary> /// <param name="source"></param> /// <param name="propertyName"></param> /// <returns></returns> public static bool TokenAsBool(this JObject source, string propertyName) { var value = source.TokenValue(propertyName); if (BaseFunc.IsNull(value)) { return(false); } else { return(BaseFunc.CBool(value.ToString())); } }
/// <summary> /// 取得指定名稱的日期時間值 /// </summary> /// <param name="source"></param> /// <param name="propertyName"></param> /// <returns></returns> public static DateTime TokenAsDateTime(this JObject source, string propertyName) { var value = source.TokenValue(propertyName); if (BaseFunc.IsNull(value)) { return(DateTime.MinValue); } else { return(BaseFunc.CDateTime(value.ToString())); } }
/// <summary> /// 取得符合指定巢狀結構的XElement /// </summary> /// <param name="elem"></param> /// <param name="names"></param> /// <returns></returns> public static XElement NestElement(this XElement elem, params XName[] names) { XElement result = elem; foreach (var name in names) { result = result.Element(name); if (BaseFunc.IsNull(result)) { break; } } return(result); }
/// <summary> /// 轉型為文字。 /// </summary> /// <param name="value">要轉型的值。</param> /// <param name="defaultValue">無法成功轉型的預設值。</param> public static string CStr(object value, string defaultValue) { //若為 Null 或 DBNull 值,則傳回預設值 if (BaseFunc.IsNull(value)) { return(defaultValue); } //若為列舉型別,則傳回列舉名稱 if (value is Enum) { return(GetEnumName((Enum)value)); } //轉型為文字 //return (string)value; return(Convert.ToString(value)); }
/// <summary> /// 對目的資料表新增來源資料表中的資料列,有相同欄位時才會塞值。 /// </summary> /// <param name="destDataTable">目的資料表。</param> /// <param name="sourceTable">來源資料表。</param> /// <param name="allowNull">是否允許Null值</param> public static void AddTableRows(DataTable destDataTable, DataTable sourceTable, bool allowNull = true) { if (BaseFunc.IsNull(destDataTable)) { return; } if (DataFunc.IsEmpty(sourceTable)) { return; } foreach (DataRowView sourceRow in sourceTable.DefaultView) { AddRow(destDataTable, sourceRow.Row, allowNull); } }
/// <summary> /// 判斷資料表是否無資料,為 Null 或資料列數為零,皆視為無資料。 /// </summary> /// <param name="dataTable">要判斷的資料表。</param> /// <param name="isDefaultViewCount">是否以 DefaultView 做為資料筆數的判斷基準。</param> public static bool IsEmpty(DataTable dataTable, bool isDefaultViewCount = true) { if (BaseFunc.IsNull(dataTable)) { return(true); } // 判斷資料筆數是否為零 if (isDefaultViewCount) { return(dataTable.DefaultView.Count == 0); } else { return(dataTable.Rows.Count == 0); } }
/// <summary> /// 判斷資料集是否無資料,為 Null 或資料表數為零,皆視為無資料。 /// </summary> /// <param name="dataSet">要判斷的資料表。</param> public static bool IsEmpty(DataSet dataSet) { //資料集為 Null 或無資料表,皆視為無資料 if (BaseFunc.IsNull(dataSet) || (dataSet.Tables.Count == 0)) { return(true); } // 主檔資料表無資料時,也視為無資料 if (StrFunc.StrIsNotEmpty(dataSet.DataSetName) && dataSet.Tables.Contains(dataSet.DataSetName)) { if (IsEmpty(dataSet.Tables[dataSet.DataSetName])) { return(true); } } return(false); }
/// <summary> /// 計算欄位值總和 /// </summary> /// <param name="row">取值計算的資料列。</param> /// <param name="fieldNames">欄位名稱陣列。</param> public static double SumFieldValues(DataRow row, params string[] fieldNames) { double sum = 0; if (BaseFunc.IsNull(row)) { return(sum); } foreach (var fieldName in fieldNames) { if (row.RowState != DataRowState.Deleted && HasField(row, fieldName)) { sum += BaseFunc.CDouble(row[fieldName]); } } return(sum); }
/// <summary> /// 判斷檢視表是否無資料,Null 或資料列數為零,皆視為無資料。 /// </summary> /// <param name="dataView">要判斷的檢視表。</param> public static bool IsEmpty(DataView dataView) { //檢視表為 Null 或資料列數為零,皆視為無資料 return(BaseFunc.IsNull(dataView) || (dataView.Count == 0)); }