Esempio n. 1
0
        public static void BackupTable(string tablename, BackupFile file, TableDefn table)
        {
            file.AppendLine("  <" + Pluralizer.Pluralize(tablename) + ">");

            string query = "SELECT " + table.SelectCols() + " FROM " + tablename + ";";

            IDbConnection dbConn = Connection.Me.GetConnection();

            try
            {
                dbConn.Open();
                IDbCommand dbCmd = dbConn.CreateCommand();
                dbCmd.CommandType = CommandType.Text;
                dbCmd.CommandText = query;

                using (IDataReader rdr = dbCmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        file.AppendLine("    <" + tablename + ">");
                        for (int i = 0; i < rdr.FieldCount; i++)
                        {
                            //Console.WriteLine(rdr.GetName(i) + "=" + rdr.GetValue(i).ToString());
                            string key      = rdr.GetName(i);
                            object fldvalue = rdr.GetValue(i);
                            if (fldvalue != DBNull.Value)
                            {
                                fldvalue = Convert.ChangeType(fldvalue, rdr.GetFieldType(i));
                                string clrtype = table.FindColumn(key).CLRType().Name;
                                if (fldvalue != null)
                                {
                                    file.AppendLine("      <" + key + ">" + FormatValue(fldvalue, clrtype) + "</" + key + ">");
                                }
                            }
                        }
                        file.AppendLine("    </" + tablename + ">");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("There was a problem loading table from query [" + query + "] - " + ex.Message, ex);
            }
            finally
            {
                dbConn.Close();
                dbConn = null;
            }

            file.AppendLine("  </" + Pluralizer.Pluralize(tablename) + ">");
        }
Esempio n. 2
0
        public static void ReportTable(string tablename, TableDefn table, int width)
        {
            string query = "SELECT " + table.SelectCols() + " FROM " + tablename + ";";

            int           capacity = 4096;
            StringBuilder sb       = new StringBuilder(capacity);
            int           rows     = 0;

            IDbConnection dbConn = Connection.Me.GetConnection();

            try
            {
                dbConn.Open();
                IDbCommand dbCmd = dbConn.CreateCommand();
                dbCmd.CommandType = CommandType.Text;
                dbCmd.CommandText = query;

                using (IDataReader rdr = dbCmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        ++rows;
                        for (int i = 0; i < rdr.FieldCount; i++)
                        {
                            object fldvalue = rdr.GetValue(i);
                            if (fldvalue != DBNull.Value)
                            {
                                string value = Convert.ChangeType(fldvalue, rdr.GetFieldType(i)).ToString().TrimEnd();
                                string type  = table.FindColumn(rdr.GetName(i)).ColumnType;
                                if (type == "BIT")
                                {
                                    if (value == "0" || value.ToLower() == "false")
                                    {
                                        value = "false";
                                    }
                                    else if (value == "1" || value.ToLower() == "true")
                                    {
                                        value = "true";
                                    }
                                    else
                                    {
                                        Console.WriteLine(value);
                                    }
                                }
                                else if (type == "DATETIME")
                                {
                                    value = value.Left(10);
                                }
                                if (sb.Length + value.Length > capacity)
                                {
                                    capacity = capacity * 2;
                                    sb.EnsureCapacity(capacity);
                                }
                                sb.Append(EscapeXML(value));
                            }
                        }
                    }
                }

                if (rows > 0)
                {
                    string hash = MakeHash(sb.ToString());
                    Console.WriteLine(tablename.PadRight(width) + " has " + rows.ToString().PadRight(5) + " rows and hash=" + hash);
                }
                else
                {
                    Console.WriteLine(tablename.PadRight(width) + " is empty.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("There was a problem loading table from query [" + query + "] - " + ex.Message, ex);
            }
            finally
            {
                dbConn.Close();
                dbConn = null;
            }
        }