/// <summary>
        /// 读取
        /// </summary>
        /// <returns></returns>
        public ObjectTableStorage Read()
        {
            using (StreamReader reader = new StreamReader(Stream, this.Encoding))
            {
                //记录第一次出现的数据类型,后续依此转换
                var name = Path.GetFileNameWithoutExtension(FilePath);

                ObjectTableStorage storage = new ObjectTableStorage(name);

                string  [] titles = null;
                var        line   = "";
                int        index  = -1;
                while ((line = reader.ReadLine()) != null)
                {
                    if (String.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    index++;
                    if (index == 0)//tittle
                    {
                        titles = line.Split(Spliters, StringSplitOptions);
                        ColTypes.InitNames(titles);
                        continue;
                    }

                    var vals = line.Split(Spliters, StringSplitOptions);//, StringSplitOptions.RemoveEmptyEntries);
                    storage.NewRow();
                    int length = Math.Min(titles.Length, vals.Length);
                    for (int i = 0; i < length; i++)
                    {
                        var    title  = titles[i].Trim();
                        var    type   = ColTypes[title];
                        var    val    = vals[i];
                        object newVal = null;
                        if (!Appeared.Contains(i))
                        {
                            type.ValueType = ValueTypeHelper.GetValueType(val);
                            Appeared.Add(i);
                        }
                        if (type.ValueType == ValueType.Unknown)
                        {
                            type.ValueType = ValueTypeHelper.GetValueType(val);
                        }

                        newVal = type.GetValue(val);

                        if (newVal != null)//更新类型
                        {
                            // type.ValueType = ValueTypeHelper.GetValueType(currentVal);
                            storage.AddItem(title, newVal);
                        }
                    }
                    //storage.AddItem(titles, vals);
                    storage.EndRow();
                }
                return(storage);
            }
        }
示例#2
0
        /// <summary>
        /// Sets the column type
        /// Will setup the class variable based on the enum name
        /// </summary>
        /// <param name="coltype">The type of colum to setup this class with</param>
        public void SetColType(ColTypes coltype)
        {
            switch (coltype)
            {
            case ColTypes.VarChar:
                s_ColType = "VARCHAR(255)";
                break;

            case ColTypes.Int:
                s_ColType = "INT";
                break;

            case ColTypes.Double:
                s_ColType = "DOUBLE(10, 2)";
                break;

            case ColTypes.Date:
                s_ColType = "DATE()";
                break;

            case ColTypes.AutoIncrement:
                s_ColType = "INT PRIMARY KEY IDENTITY(1,1)";
                break;
            }
        }
示例#3
0
        public static String ConvertToSql(ColTypes type, object value)
        {
            String sql = "";

            if (value == null || value.ToString().Equals(""))
            {
                sql = "NULL";
            }
            else
            {
                switch (type)
                {
                case ColTypes.Text:
                    sql = "'" + value.ToString().Replace("'", "") + "'";
                    break;

                case ColTypes.Numeric:
                    sql = value.ToString();
                    break;

                case ColTypes.Date:
                    sql = DateToSql((DateTime)value);
                    break;

                case ColTypes.Boolean:
                    sql = value.ToString();
                    break;
                }
            }

            return(sql);
        }
示例#4
0
        private string s_Colname;        //the name of the column to access it
        #endregion

        #region public TableEntry(ColTypes coltype, string colname)
        /// <summary>
        /// Constructor for the TableEntry class
        /// Sets up the column type and the column name
        /// </summary>
        /// <param name="coltype">The column type definition, must be the enum</param>
        /// <param name="colname">The name of the column which will be accessible through the DB</param>
        public TableEntry(ColTypes coltype, string colname)
        {
            SetColType(coltype);        //set the column type
            s_Colname = colname;        //set the column name
        }