Ejemplo n.º 1
0
        public ColumnMetaCollection GetTableSchema(string table)
        {
            Dictionary <string, string> comments   = GetColumnComment(table);
            ColumnMetaCollection        metas      = new ColumnMetaCollection();
            MySqlConnection             connection = new MySqlConnection(this.ConnectionString);

            connection.Open();



            MySqlCommand cmd = new MySqlCommand("select * from " + table + " limit 0,1", connection);

            using (MySqlDataReader ddr = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.CloseConnection))
            {
                if (ddr != null && !ddr.IsClosed)
                {
                    //得元数据
                    DataTable dt = ddr.GetSchemaTable();
                    for (int i = 0, len = dt.Rows.Count; i < len; i++)
                    {
                        ColumnMeta columnMeta = new ColumnMeta();

                        columnMeta.ColumnName     = dt.Rows[i][0].ToString();
                        columnMeta.ColumnOridinal = TypeConverter.ObjectToInt(dt.Rows[i][1], 0);
                        columnMeta.ColumnSize     = TypeConverter.ObjectToInt(dt.Rows[i][2], 4);
                        columnMeta.IsUnique       = TypeConverter.ObjectToBool(dt.Rows[i][5], false);
                        columnMeta.IsKey          = TypeConverter.ObjectToBool(dt.Rows[i][6], false);

                        columnMeta.FieldType     = (Type)dt.Rows[i][11];
                        columnMeta.FieldTypeName = columnMeta.FieldType.Name;
                        columnMeta.AllowDBNull   = TypeConverter.ObjectToBool(dt.Rows[i][12], true);
                        if (comments.ContainsKey(columnMeta.ColumnName))
                        {
                            columnMeta.Comment = comments[columnMeta.ColumnName];
                            if (!string.IsNullOrEmpty(columnMeta.Comment))
                            {
                                columnMeta.Comment = columnMeta.Comment.Replace("\r", "").Replace("\n", "");
                            }
                        }



                        if (dt.Rows[i][0].ToString().IndexOf(" ") > -1)
                        {
                            continue;
                        }

                        metas.Add(columnMeta);
                    }
                }
            }

            connection.Close();
            return(metas);
        }
Ejemplo n.º 2
0
        public ColumnMetaCollection GetTableSchema(string table)
        {
            ColumnMetaCollection metas      = new ColumnMetaCollection();
            SqlConnection        connection = new SqlConnection(this.ConnectionString);

            connection.Open();

            SqlCommand cmd = new SqlCommand("select * from " + table, connection);

            using (SqlDataReader ddr = cmd.ExecuteReader(CommandBehavior.KeyInfo))
            {
                if (ddr != null && !ddr.IsClosed)
                {
                    //得元数据
                    DataTable dt = ddr.GetSchemaTable();

                    //StringBuilder builder = new StringBuilder();
                    for (int i = 0, len = dt.Rows.Count; i < len; i++)
                    {
                        //for (int offset = 0; offset < dt.Columns.Count; offset++)
                        //{
                        //    builder.AppendLine(dt.Columns[offset].ColumnName+"=>(index:"+offset+")=>"+dt.Rows[i][offset]);
                        //}


                        ColumnMeta columnMeta = new ColumnMeta();

                        columnMeta.ColumnName     = dt.Rows[i][0].ToString();
                        columnMeta.ColumnOridinal = TypeConverter.ObjectToInt(dt.Rows[i][1], 0);
                        columnMeta.ColumnSize     = TypeConverter.ObjectToInt(dt.Rows[i][2], 4);
                        columnMeta.IsUnique       = TypeConverter.ObjectToBool(dt.Rows[i][5], false);
                        columnMeta.IsKey          = TypeConverter.ObjectToBool(dt.Rows[i][6], false);


                        columnMeta.FieldTypeName = ((Type)dt.Rows[i][12]).Name;
                        columnMeta.FieldType     = (Type)dt.Rows[i][12];
                        columnMeta.AllowDBNull   = TypeConverter.ObjectToBool(dt.Rows[i][13], true);



                        if (dt.Rows[i][0].ToString().IndexOf(" ") > -1)
                        {
                            continue;
                        }

                        metas.Add(columnMeta);
                    }

                    //File.WriteAllText("d:\\struct.txt", builder.ToString());
                }
            }
            return(metas);
        }