Advertisement
Ladies_Man

cdb almost 2nd stage

Mar 18th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Collections.Generic;
  7. using System.Threading.Tasks;
  8. using System.Collections;
  9. using System.Diagnostics;
  10.  
  11. using System.Data.SQLite;
  12.  
  13. using MySql.Data;
  14. using MySql.Data.MySqlClient;
  15.  
  16. namespace adonet_4
  17. {
  18.     public class Globals
  19.         {
  20.             private static SortedDictionary<string, string> _mapNameToData = new SortedDictionary<string, string>();
  21.             private static List<string> _listTablenames;
  22.  
  23.             public static SortedDictionary<string, string> mapNameToData
  24.             {
  25.                 get { return _mapNameToData; }
  26.                 set { _mapNameToData = value; }
  27.             }
  28.  
  29.             public static List<string> listTableNames
  30.             {
  31.                 get { return _listTablenames; }
  32.                 set { _listTablenames = value; }
  33.             }
  34.         }
  35.  
  36.     class Program
  37.     {
  38.  
  39.         static void execReaderSqlite(SQLiteCommand command)
  40.         {
  41.             try
  42.             {
  43.                 SQLiteDataReader reader = command.ExecuteReader();
  44.                 Console.WriteLine("Executing Sqlite reader for query:" + command.CommandText);
  45.                 do
  46.                 {
  47.                     while (reader.Read())
  48.                     {
  49.                         Object[] values = new Object[reader.FieldCount];
  50.                         int fieldCount = reader.GetValues(values);
  51.  
  52.                         for (int i = 0; i < fieldCount; i++)
  53.                         {
  54.                             Console.Write("\t{0}", values[i]);
  55.                         }
  56.                         Console.WriteLine();
  57.                     }
  58.  
  59.                 } while (reader.NextResult());
  60.  
  61.                 reader.Close();
  62.  
  63.             }
  64.             catch (Exception e)
  65.             {
  66.                 Console.WriteLine(e.Message);
  67.             }
  68.  
  69.             Console.WriteLine();
  70.         }
  71.  
  72.         static void execReaderMysql(MySqlCommand command)
  73.         {
  74.             try
  75.             {
  76.                 MySqlDataReader reader = command.ExecuteReader();
  77.                 Console.WriteLine("Executing Mysql reader for query:" + command.CommandText);
  78.                 do
  79.                 {
  80.                     while (reader.Read())
  81.                     {
  82.                         Object[] values = new Object[reader.FieldCount];
  83.                         int fieldCount = reader.GetValues(values);
  84.  
  85.                         for (int i = 0; i < fieldCount; i++)
  86.                         {
  87.                             Console.Write("\t{0}", values[i]);
  88.                            
  89.                         }
  90.                         Console.WriteLine();
  91.                     }
  92.  
  93.                 } while (reader.NextResult());
  94.  
  95.                 reader.Close();
  96.  
  97.             }
  98.             catch (Exception e)
  99.             {
  100.                 Console.WriteLine(e.Message);
  101.             }
  102.  
  103.             Console.WriteLine();
  104.         }
  105.  
  106.  
  107.  
  108.  
  109.         static List<string> retrieveTablenames(SQLiteConnection connection)
  110.         {
  111.             List<string> tmpList = new List<string>();
  112.  
  113.             try
  114.             {
  115.                
  116.                 string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
  117.                 SQLiteCommand cmd = new SQLiteCommand(q, connection);
  118.  
  119.                 SQLiteDataReader reader = cmd.ExecuteReader();
  120.  
  121.                 if (reader.HasRows)
  122.                 {
  123.                     int j = 0;
  124.                     while (reader.Read())
  125.                     {
  126.                         for (int i = 0; i < reader.FieldCount; i++)
  127.                         {
  128.                             string tableName = reader.GetValue(i).ToString();
  129.  
  130.                             tmpList.Add(tableName);
  131.                         }
  132.                         j++;
  133.                     }
  134.                 }
  135.             }
  136.             catch (Exception e)
  137.             {
  138.                 Console.WriteLine(e.Message);
  139.                 Console.ReadLine();
  140.             }
  141.  
  142.             return tmpList;
  143.         }
  144.  
  145.  
  146.         static string retrieveData(string tableName, SQLiteConnection connection)
  147.         {
  148.  
  149.             string q = string.Format("SELECT * FROM {0}", tableName);
  150.             SQLiteCommand cmd = new SQLiteCommand(q, connection);
  151.  
  152.             string entry = "";
  153.  
  154.             try {
  155.                 if (null != connection && ConnectionState.Open != connection.State)
  156.                 {
  157.                     connection.Open();
  158.                 }
  159.                 //string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
  160.                 //q = "select * from Persons";
  161.                 //SQLiteCommand cmd = new SQLiteCommand(query, connection);
  162.  
  163.                 SQLiteDataReader reader = cmd.ExecuteReader();
  164.  
  165.                
  166.                 int entryNum = 0;
  167.  
  168.                 if (reader.HasRows)
  169.                 {
  170.                     Console.WriteLine("reading table [" + tableName + "]");
  171.                     while (reader.Read())
  172.                     {
  173.                         //object[] values = new object[reader.FieldCount];
  174.                         //reader.GetValues(values);
  175.                         //rowList.Add(values);
  176.                        
  177.                         entry += "(";
  178.  
  179.                         for (int i = 0; i < reader.FieldCount; i++)
  180.                         {
  181.                             //Console.Write("\t{0}", Convert.ToString(reader[i]));
  182.                             try
  183.                             {
  184.                                 string cellType = reader.GetDataTypeName(i).ToString();
  185.                                 if ("INTEGER" != cellType)
  186.                                 {
  187.                                     entry += "\"";
  188.                                 }
  189.                                 //
  190.                                 //DATETIME
  191.                                 //
  192.                                 string cellValue = reader.GetValue(i).ToString();
  193.                                 entry += cellValue;
  194.  
  195.  
  196.                                 if ("INTEGER" != cellType)
  197.                                 {
  198.                                     entry += "\"";
  199.                                 }
  200.  
  201.                                 entry += ", ";
  202.  
  203.                                
  204.                             }
  205.                             catch (Exception e)
  206.                             {
  207.                                 Console.WriteLine(e.Message);
  208.                             }
  209.                            
  210.  
  211.                             //string v = reader[i].ToString() + ", ";
  212.                             //Console.Write(v);
  213.  
  214.                             //entry += v;
  215.                             //Globals.map.Add(values[i].ToString(), "-");
  216.                         }
  217.  
  218.                         entry = entry.Remove(entry.Length - 2);
  219.                         entry += "), ";
  220.  
  221.                         entryNum++;
  222.  
  223.                         if (entryNum == 15000)
  224.                         {
  225.                             break;
  226.                         }
  227.                         //Console.WriteLine(entry);
  228.  
  229.                         //Console.WriteLine();
  230.                     }
  231.                 }
  232.  
  233.                 if (!entry.Equals(""))
  234.                     entry = entry.Remove(entry.Length - 2);
  235.  
  236.                 reader.Close();
  237.  
  238.                 Console.WriteLine(entryNum + " entries for table '" + tableName + "':");
  239.                 Console.WriteLine(entry);
  240.  
  241.                
  242.                
  243.             }
  244.             catch (SQLiteException e)
  245.             {
  246.                 Console.WriteLine(e.Message);
  247.                 Console.ReadLine();
  248.             }
  249.             finally
  250.             {
  251.                 connection.Close();
  252.             }
  253.  
  254.             return entry;
  255.         }
  256.        
  257.  
  258.  
  259.  
  260.         static void Main(string[] args)
  261.         {
  262.             //============================================================================
  263.             //---------------------------------SQLITE-------------------------------------
  264.             //============================================================================
  265.                 string connStringSqlite =
  266.                 using (SQLiteConnection conn1 = new SQLiteConnection(connStringSqlite))
  267.                 {
  268.                     try
  269.                     {
  270.                         conn1.Open();
  271.                         Console.WriteLine("----");
  272.  
  273.                         Globals.listTableNames = retrieveTablenames(conn1);
  274.                        
  275.  
  276.                         Console.WriteLine("----");
  277.  
  278.                         Stopwatch stopWatch = new Stopwatch();
  279.                         stopWatch.Start();
  280.                         //string s = retrieveData("Submissions", conn1);
  281.                         //Globals.mapNameToData.Add("Persons", s);
  282.                         int i = 0;
  283.                         foreach (string table in Globals.listTableNames)
  284.                         {
  285.                             //Console.WriteLine("t[" + i++ + "]:" + table);
  286.                             if (!table.Equals("Submissions") &&
  287.                                 !table.Equals("Comments") &&
  288.                                 !table.Equals("Approvements") &&
  289.                                 !table.Equals("Sessions"))
  290.                             {
  291.                                 string data = retrieveData(table, conn1);
  292.                                 Globals.mapNameToData.Add(table, data);
  293.                             }
  294.                            
  295.                         }
  296.                         stopWatch.Stop();
  297.  
  298.                         //1000 -> 0.585
  299.                         //5k -> 21
  300.                         //10k -> 1:46 (100)
  301.                         //20k -> 8:36 (500)
  302.                         //25k -> 14:18 (850)
  303.  
  304.                         Console.WriteLine("elapsed time:" + stopWatch.Elapsed.ToString());
  305.  
  306.                     }
  307.                     catch (Exception e)
  308.                     {
  309.                         Console.WriteLine(e.Message);
  310.                         Console.ReadLine();
  311.                     }
  312.                 }
  313.  
  314.                 /*
  315.                 //conn1.Open();
  316.                 Console.WriteLine("connection to SQLite has been set");
  317.  
  318.  
  319.  
  320.                 //string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
  321.                 //SQLiteCommand cmd1 = new SQLiteCommand(q, conn1);
  322.                 //execReaderSqlite(cmd1);
  323.  
  324.  
  325.                 Console.WriteLine("asdasd");
  326.  
  327.                
  328.  
  329.                 Console.WriteLine("yuhjhg");
  330.  
  331.                 try
  332.                 {
  333.                     //conn1.Open();
  334.                     string tt = retrieveData("Persons", conn1);
  335.                 }
  336.                 catch (Exception e)
  337.                 {
  338.                     Console.WriteLine(e.Message);
  339.                     Console.ReadLine();
  340.                 }
  341.                
  342.  
  343.                 Console.WriteLine("12143");
  344.  
  345.                 int i = 0;
  346.                 foreach (string s in Globals.listTableNames)
  347.                 {
  348.                     Console.WriteLine("t[" + i++ + "]:" + s);
  349.  
  350.                     string tdt = "";
  351.                     try
  352.                     {
  353.                         if (conn1.State == ConnectionState.Closed)
  354.                         {
  355.                             conn1.Open();
  356.                         }
  357.                         conn1.Dispose();
  358.  
  359.                         //tdt = retrieveData(s, conn1);
  360.                         //Globals.mapNameToData.Add(s, tdt);
  361.                     }
  362.                     catch (Exception e)
  363.                     {
  364.                         Console.WriteLine("!!" + e.Message);
  365.                     }
  366.                 }
  367.  
  368.                 conn1.Dispose();
  369.  
  370.  
  371.  
  372.  
  373.  
  374.                
  375.  
  376.                 Globals.mapNameToData.Add("keystr", "valstr");
  377.  
  378.                 foreach (KeyValuePair<string, string> entry in Globals.mapNameToData)
  379.                 {
  380.                     Console.WriteLine(entry.Key + ":" + entry.Value);
  381.                 }*/
  382.  
  383.  
  384.             //============================================================================
  385.             //----------------------------------MYSQL-------------------------------------
  386.             //============================================================================
  387.  
  388.            
  389.             string connStringMysql =
  390.             MySqlConnection conn2 = new MySqlConnection(connStringMysql);
  391.  
  392.             try
  393.             {
  394.                 conn2.Open();
  395.                 Console.WriteLine("all good. mysql");
  396.             }
  397.             catch (SQLiteException e)
  398.             {
  399.                 Console.WriteLine(e.Message);
  400.             }
  401.  
  402.             MySqlCommand cmd2 = new MySqlCommand("select table_name from information_schema.tables", conn2);
  403.             execReaderMysql(cmd2);
  404.  
  405.             Console.WriteLine("cmd2 done");
  406.  
  407.             MySqlCommand cmd3 = new MySqlCommand("select * from persons", conn2);
  408.             execReaderMysql(cmd3);
  409.  
  410.             Console.WriteLine("cmd3 done");
  411.  
  412.             string q4 = "insert into persons values " + Globals.mapNameToData["Persons"];
  413.             Console.WriteLine(q4);
  414.             MySqlCommand cmd4 = new MySqlCommand(q4, conn2);
  415.             cmd4.ExecuteNonQuery();
  416.             Console.WriteLine("cmd4 done");
  417.  
  418.             execReaderMysql(cmd3);
  419.  
  420.             conn2.Dispose();
  421.  
  422.  
  423.             Console.WriteLine("all done");
  424.             Console.ReadLine();
  425.         }
  426.     }
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement