Пример #1
0
        /// <summary>
        /// 建立欄位並加入資料表中。
        /// </summary>
        /// <param name="fieldName">欄位名稱。</param>
        /// <param name="caption">欄位標題。</param>
        /// <param name="dataType">資料型別。</param>
        /// <param name="defaultValue">預設值。</param>
        /// <param name="dateTimeMode">設定資料行的 DateTimeMode。</param>
        public DataColumn AddColumn(string fieldName, string caption, Type dataType, object defaultValue, DataSetDateTime dateTimeMode = DataSetDateTime.Unspecified)
        {
            DataColumn oDataColumn;
            string     sFieldName;

            // 欄位名稱全轉為大寫
            sFieldName  = StrFunc.StrUpper(fieldName);
            oDataColumn = new DataColumn(sFieldName, dataType);
            oDataColumn.DefaultValue = defaultValue;

            if (dataType == typeof(DateTime))
            {
                oDataColumn.DateTimeMode = dateTimeMode;
            }

            if (!BaseFunc.IsDBNull(defaultValue))
            {
                oDataColumn.AllowDBNull = false;
            }

            if (StrFunc.StrIsNotEmpty(caption))
            {
                oDataColumn.Caption = caption;
            }

            this.DataTable.Columns.Add(oDataColumn);
            return(oDataColumn);
        }
Пример #2
0
        /// <summary>
        /// 取得無 DbNull 的欄位值,組成以逗號分隔的字串
        /// </summary>
        /// <param name="rows">資料列陣列</param>
        /// <param name="fieldName">欄位名稱</param>
        /// <param name="checkEmpty">檢查空白</param>
        /// <param name="isSqlStr">是否用單引號包起來</param>
        /// <param name="distinct">是否過濾同樣資料</param>
        /// <returns></returns>
        public static string GetFieldValues(IEnumerable <DataRow> rows, string fieldName, bool checkEmpty = true, bool isSqlStr = true, bool distinct = false)
        {
            string result = string.Empty;

            if (rows.Count() != 0)
            {
                if (HasField(rows.First(), fieldName))
                {
                    foreach (DataRow row in rows)
                    {
                        if (row.RowState != DataRowState.Deleted)
                        {
                            string value = BaseFunc.CStr(GetFieldValueNotDbNull(row, fieldName));
                            if (checkEmpty && StrFunc.StrIsEmpty(value))
                            {
                                continue;
                            }
                            else
                            {
                                value = isSqlStr ? StrFunc.SQLStr(value) : value;
                                if (distinct && StrFunc.StrContains(result, value))
                                {
                                    continue;
                                }
                                result += (StrFunc.StrIsNotEmpty(result) ? "," : "") + value;
                            }
                        }
                    }
                }
            }
            return(result);
        }
Пример #3
0
 /// <summary>
 /// 依分隔符號合併二個字串。
 /// </summary>
 /// <param name="s1">第一個字串。</param>
 /// <param name="s2">第二個字串。</param>
 /// <param name="delimiter">分隔符號。</param>
 public static string StrMerge(string s1, string s2, string delimiter)
 {
     if (StrFunc.StrIsNotEmpty(s1))
     {
         s1 = s1 + delimiter;
     }
     return(s1 + s2);
 }
Пример #4
0
 /// <summary>
 /// 將字串前後加上單引號
 /// </summary>
 /// <param name="source"></param>
 /// <returns></returns>
 public static string SQLStr(this string source)
 {
     if (StrFunc.StrIsNotEmpty(source))
     {
         return(StrFunc.SQLStr(source));
     }
     else
     {
         return("''");
     }
 }
Пример #5
0
        /// <summary>
        /// 轉型成時間間格值
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static TimeSpan CTimeSpan(string time)
        {
            var result = new TimeSpan();

            if (StrFunc.StrIsNotEmpty(time))
            {
                int hours   = BaseFunc.CInt(StrFunc.StrLeft(time, 2));
                int minutes = BaseFunc.CInt(StrFunc.StrRight(time, 2));
                result = new TimeSpan(hours, minutes, 0);
            }

            return(result);
        }
Пример #6
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);
        }