Exemplo n.º 1
0
        public static void LoadTableField(string conn, Dictionary <string, List <DBTable> > list, ProgressBar pBar)
        {
            try
            {
                pBar.Minimum = 0;
                pBar.Maximum = list.Count;

                Thread thread = new Thread(delegate()
                {
                    int current = 0;
                    foreach (var t in list)
                    {
                        DifferentForm.BeginInvoke(new MethodInvoker(delegate
                        {
                            pBar.Value = current;
                        }));

                        string sql   = string.Empty;
                        DataTable dt = null;

                        string dbType = GetDBType(conn);
                        switch (dbType)
                        {
                        case Data.XmlMysql:
                            #region MySql数据库读取每张表和视图的字段
                            if (t.Key.StartsWith(ViewFlag))
                            {
                                sql = "SELECT * FROM user_tab_cols WHERE table_name ='" + t.Key.Replace(ViewFlag, "") + "'";
                            }
                            else
                            {
                                sql = "SHOW FULL COLUMNS FROM " + t.Key;
                            }
                            dt = new MySqlHelper().ExecuteDataTable(conn, CommandType.Text, sql, null);
                            foreach (var field in dt.AsEnumerable().ToList())
                            {
                                DBTable item = new DBTable();
                                item.Field   = field["Field"].Ustring();
                                int loc      = field["Type"].Ustring().IndexOf("(");
                                if (loc > 0)
                                {
                                    item.Type   = field["Type"].ToString().Substring(0, loc);
                                    item.Length = field["Type"].ToString().Substring(loc + 1).TrimEnd(')').Uint();
                                }
                                else
                                {
                                    item.Type = field["Type"].ToString();
                                }
                                item.CanNull = (field["Null"].Ustring().ToLower() == "yes" ? true : false);
                                item.Comment = field["Comment"].Ustring();
                                list[t.Key].Add(item);
                            }
                            #endregion
                            break;

                        case Data.XmlOracle:
                            if (t.Key.StartsWith(ViewFlag))
                            {
                                sql = "SELECT * FROM user_tab_cols WHERE table_name ='" + t.Key.Replace(ViewFlag, "") + "'";
                            }
                            else
                            {
                                sql = "select COLUMN_NAME,DATA_TYPE,DATA_LENGTH,NULLABLE from USER_TAB_COLS  where TABLE_NAME='" + t.Key + "'  And HIDDEN_COLUMN='NO'";
                            }
                            dt = new OracleHelper().ExecuteDataTable(conn, CommandType.Text, sql);
                            foreach (var field in dt.AsEnumerable().ToList())
                            {
                                DBTable item = new DBTable();
                                item.Field   = field["COLUMN_NAME"].ToString();
                                item.Type    = field["DATA_TYPE"].ToString();
                                item.Length  = field["DATA_LENGTH"].Uint();
                                item.CanNull = (field["NULLABLE"].Ustring() == "Y" ? true : false);
                                list[t.Key].Add(item);
                            }
                            break;

                        case Data.XmlSqlServer:
                            #region SqlServer数据库读取每张表的字段

                            sql = @"SELECT table_name,
                                        column_name,
                                        ISNULL(column_default,'') AS column_default,
                                        is_nullable,
                                        data_type,
                                        ISNULL(ISNULL(ISNULL(character_maximum_length,numeric_precision),datetime_precision),1) AS column_length
                                        FROM information_schema.columns
                                        WHERE NOT table_name IN('sysdiagrams','dtproperties') and table_name='" + t.Key +
                                  "'";

                            dt = new SqlServerHelper().ExecuteDataTable(conn, CommandType.Text, sql, null);
                            foreach (var field in dt.AsEnumerable().ToList())
                            {
                                DBTable item = new DBTable();
                                item.Field   = field["column_name"].ToString();
                                item.Type    = field["data_type"].ToString();
                                item.Length  = field["column_length"].Uint();
                                item.CanNull = (field["is_nullable"].Ustring().ToLower() == "yes" ? true : false);
                                list[t.Key].Add(item);
                            }

                            #endregion
                            break;
                        }
                        current++;
                    }
                    DifferentForm.BeginInvoke(new MethodInvoker(delegate
                    {
                        pBar.Visible = false;
                    }));
                });
                thread.IsBackground = true;
                thread.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }