コード例 #1
0
ファイル: FrmTableNew.cs プロジェクト: thinhils/Nikita
        /// <summary>根据DataGridViewRow 生成DatabaseColumn
        /// 根据DataGridViewRow 生成DatabaseColumn
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="databaseTable"></param>
        /// <returns></returns>
        public DatabaseColumn GenDatabaseColumn(DataGridViewRow dr, DatabaseTable databaseTable)
        {
            DatabaseColumn databaseColumn = new DatabaseColumn();

            databaseColumn.Table = databaseTable;
            //数据类型
            string strColumnLength = dr.Cells[colColumnLength.Name].Value == null ? string.Empty : dr.Cells[colColumnLength.Name].Value.ToString();
            string strColumnType   = dr.Cells[colColumnType.Name].Value == null ? string.Empty : dr.Cells[colColumnType.Name].Value.ToString();
            string strDataType     = string.IsNullOrEmpty(strColumnLength) ? strColumnType : strColumnType.Split('(')[0] + "(" + strColumnLength + ")";
            string strColumnName   = dr.Cells[colColumnName.Name].Value == null ? string.Empty : dr.Cells[colColumnName.Name].Value.ToString();
            string strColumnScale  = dr.Cells[colColumnScale.Name].Value == null ? string.Empty : dr.Cells[colColumnScale.Name].Value.ToString();
            bool   blnPK           = true;
            string strColumnPK     = dr.Cells[colColumnPK.Name].Value == null ? string.Empty : dr.Cells[colColumnPK.Name].Value.ToString();

            if (strColumnPK == string.Empty || strColumnPK.ToUpper() == "FALSE")
            {
                blnPK = false;
            }
            bool   blnIdentity       = true;
            string strColumnIdentity = dr.Cells[colColumnIdentity.Name].Value == null ? string.Empty : dr.Cells[colColumnIdentity.Name].Value.ToString();

            if (strColumnIdentity == string.Empty || strColumnIdentity.ToUpper() == "FALSE")
            {
                blnIdentity = false;
            }
            bool   blnAllow           = true;
            string strColumnAllowNull = dr.Cells[colColumnAllowNull.Name].Value == null ? string.Empty : dr.Cells[colColumnAllowNull.Name].Value.ToString();

            if (strColumnAllowNull == string.Empty || strColumnAllowNull.ToUpper() == "FALSE")
            {
                blnAllow = false;
            }
            string strColumnRemark       = dr.Cells[colColumnRemark.Name].Value == null ? string.Empty : dr.Cells[colColumnRemark.Name].Value.ToString();
            string strColumnDefaultValue = dr.Cells[colColumnDefaultValue.Name].Value == null ? string.Empty : dr.Cells[colColumnDefaultValue.Name].Value.ToString();

            databaseColumn.Name       = strColumnName;
            databaseColumn.DbDataType = strDataType.Split('(')[0].ToUpper();
            DatabaseReader reader = new DatabaseReader(databaseTable.DatabaseSchema);

            databaseColumn.DataType = reader.DataTypes().Where(t => t.TypeName.ToLower() == databaseColumn.DbDataType.ToLower()).FirstOrDefault();
            if (!string.IsNullOrEmpty(strColumnLength))
            {
                databaseColumn.Length = int.Parse(strColumnLength);
            }
            else
            {
                if (strColumnType.Contains('('))
                {
                    if (strColumnType.ToUpper().Contains("MAX"))
                    {
                        databaseColumn.Length = -1;
                    }
                    else
                    {
                        databaseColumn.Length = int.Parse(strColumnType.Replace(")", "").Split('(')[1].Split(',')[0]);
                    }
                }
            }

            if (blnPK)
            {
                databaseColumn.AddPrimaryKey();
            }
            if (blnIdentity)
            {
                databaseColumn.AddIdentity();
            }
            if (blnAllow)
            {
                databaseColumn.AddNullable();
            }
            if (strColumnRemark != string.Empty)
            {
                databaseColumn.Description = strColumnRemark;
            }
            if (strColumnDefaultValue != string.Empty)
            {
                databaseColumn.DefaultValue = strColumnDefaultValue;
            }
            if (strColumnScale != string.Empty)
            {
                databaseColumn.Scale = int.Parse(strColumnScale);
            }
            return(databaseColumn);
        }