Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Collections;
- using System.Diagnostics;
- using System.Data.SQLite;
- using MySql.Data;
- using MySql.Data.MySqlClient;
- namespace adonet_4
- {
- public class Globals
- {
- private static SortedDictionary<string, string> _mapNameToData = new SortedDictionary<string, string>();
- private static List<string> _listTablenames;
- public static SortedDictionary<string, string> mapNameToData
- {
- get { return _mapNameToData; }
- set { _mapNameToData = value; }
- }
- public static List<string> listTableNames
- {
- get { return _listTablenames; }
- set { _listTablenames = value; }
- }
- }
- class Program
- {
- static void execReaderSqlite(SQLiteCommand command)
- {
- try
- {
- SQLiteDataReader reader = command.ExecuteReader();
- Console.WriteLine("Executing Sqlite reader for query:" + command.CommandText);
- do
- {
- while (reader.Read())
- {
- Object[] values = new Object[reader.FieldCount];
- int fieldCount = reader.GetValues(values);
- for (int i = 0; i < fieldCount; i++)
- {
- Console.Write("\t{0}", values[i]);
- }
- Console.WriteLine();
- }
- } while (reader.NextResult());
- reader.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.WriteLine();
- }
- static void execReaderMysql(MySqlCommand command)
- {
- try
- {
- MySqlDataReader reader = command.ExecuteReader();
- Console.WriteLine("Executing Mysql reader for query:" + command.CommandText);
- do
- {
- while (reader.Read())
- {
- Object[] values = new Object[reader.FieldCount];
- int fieldCount = reader.GetValues(values);
- for (int i = 0; i < fieldCount; i++)
- {
- Console.Write("\t{0}", values[i]);
- }
- Console.WriteLine();
- }
- } while (reader.NextResult());
- reader.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.WriteLine();
- }
- static List<string> retrieveTablenames(SQLiteConnection connection)
- {
- List<string> tmpList = new List<string>();
- try
- {
- string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
- SQLiteCommand cmd = new SQLiteCommand(q, connection);
- SQLiteDataReader reader = cmd.ExecuteReader();
- if (reader.HasRows)
- {
- int j = 0;
- while (reader.Read())
- {
- for (int i = 0; i < reader.FieldCount; i++)
- {
- string tableName = reader.GetValue(i).ToString();
- tmpList.Add(tableName);
- }
- j++;
- }
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- Console.ReadLine();
- }
- return tmpList;
- }
- static string retrieveData(string tableName, SQLiteConnection connection)
- {
- string q = string.Format("SELECT * FROM {0}", tableName);
- SQLiteCommand cmd = new SQLiteCommand(q, connection);
- string entry = "";
- try {
- if (null != connection && ConnectionState.Open != connection.State)
- {
- connection.Open();
- }
- //string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
- //q = "select * from Persons";
- //SQLiteCommand cmd = new SQLiteCommand(query, connection);
- SQLiteDataReader reader = cmd.ExecuteReader();
- int entryNum = 0;
- if (reader.HasRows)
- {
- Console.WriteLine("reading table [" + tableName + "]");
- while (reader.Read())
- {
- //object[] values = new object[reader.FieldCount];
- //reader.GetValues(values);
- //rowList.Add(values);
- entry += "(";
- for (int i = 0; i < reader.FieldCount; i++)
- {
- //Console.Write("\t{0}", Convert.ToString(reader[i]));
- try
- {
- string cellType = reader.GetDataTypeName(i).ToString();
- if ("INTEGER" != cellType)
- {
- entry += "\"";
- }
- //
- //DATETIME
- //
- string cellValue = reader.GetValue(i).ToString();
- entry += cellValue;
- if ("INTEGER" != cellType)
- {
- entry += "\"";
- }
- entry += ", ";
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- //string v = reader[i].ToString() + ", ";
- //Console.Write(v);
- //entry += v;
- //Globals.map.Add(values[i].ToString(), "-");
- }
- entry = entry.Remove(entry.Length - 2);
- entry += "), ";
- entryNum++;
- if (entryNum == 15000)
- {
- break;
- }
- //Console.WriteLine(entry);
- //Console.WriteLine();
- }
- }
- if (!entry.Equals(""))
- entry = entry.Remove(entry.Length - 2);
- reader.Close();
- Console.WriteLine(entryNum + " entries for table '" + tableName + "':");
- Console.WriteLine(entry);
- }
- catch (SQLiteException e)
- {
- Console.WriteLine(e.Message);
- Console.ReadLine();
- }
- finally
- {
- connection.Close();
- }
- return entry;
- }
- static void Main(string[] args)
- {
- //============================================================================
- //---------------------------------SQLITE-------------------------------------
- //============================================================================
- string connStringSqlite =
- using (SQLiteConnection conn1 = new SQLiteConnection(connStringSqlite))
- {
- try
- {
- conn1.Open();
- Console.WriteLine("----");
- Globals.listTableNames = retrieveTablenames(conn1);
- Console.WriteLine("----");
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- //string s = retrieveData("Submissions", conn1);
- //Globals.mapNameToData.Add("Persons", s);
- int i = 0;
- foreach (string table in Globals.listTableNames)
- {
- //Console.WriteLine("t[" + i++ + "]:" + table);
- if (!table.Equals("Submissions") &&
- !table.Equals("Comments") &&
- !table.Equals("Approvements") &&
- !table.Equals("Sessions"))
- {
- string data = retrieveData(table, conn1);
- Globals.mapNameToData.Add(table, data);
- }
- }
- stopWatch.Stop();
- //1000 -> 0.585
- //5k -> 21
- //10k -> 1:46 (100)
- //20k -> 8:36 (500)
- //25k -> 14:18 (850)
- Console.WriteLine("elapsed time:" + stopWatch.Elapsed.ToString());
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- Console.ReadLine();
- }
- }
- /*
- //conn1.Open();
- Console.WriteLine("connection to SQLite has been set");
- //string q = "SELECT name FROM sqlite_master WHERE type = 'table'";
- //SQLiteCommand cmd1 = new SQLiteCommand(q, conn1);
- //execReaderSqlite(cmd1);
- Console.WriteLine("asdasd");
- Console.WriteLine("yuhjhg");
- try
- {
- //conn1.Open();
- string tt = retrieveData("Persons", conn1);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- Console.ReadLine();
- }
- Console.WriteLine("12143");
- int i = 0;
- foreach (string s in Globals.listTableNames)
- {
- Console.WriteLine("t[" + i++ + "]:" + s);
- string tdt = "";
- try
- {
- if (conn1.State == ConnectionState.Closed)
- {
- conn1.Open();
- }
- conn1.Dispose();
- //tdt = retrieveData(s, conn1);
- //Globals.mapNameToData.Add(s, tdt);
- }
- catch (Exception e)
- {
- Console.WriteLine("!!" + e.Message);
- }
- }
- conn1.Dispose();
- Globals.mapNameToData.Add("keystr", "valstr");
- foreach (KeyValuePair<string, string> entry in Globals.mapNameToData)
- {
- Console.WriteLine(entry.Key + ":" + entry.Value);
- }*/
- //============================================================================
- //----------------------------------MYSQL-------------------------------------
- //============================================================================
- string connStringMysql =
- MySqlConnection conn2 = new MySqlConnection(connStringMysql);
- try
- {
- conn2.Open();
- Console.WriteLine("all good. mysql");
- }
- catch (SQLiteException e)
- {
- Console.WriteLine(e.Message);
- }
- MySqlCommand cmd2 = new MySqlCommand("select table_name from information_schema.tables", conn2);
- execReaderMysql(cmd2);
- Console.WriteLine("cmd2 done");
- MySqlCommand cmd3 = new MySqlCommand("select * from persons", conn2);
- execReaderMysql(cmd3);
- Console.WriteLine("cmd3 done");
- string q4 = "insert into persons values " + Globals.mapNameToData["Persons"];
- Console.WriteLine(q4);
- MySqlCommand cmd4 = new MySqlCommand(q4, conn2);
- cmd4.ExecuteNonQuery();
- Console.WriteLine("cmd4 done");
- execReaderMysql(cmd3);
- conn2.Dispose();
- Console.WriteLine("all done");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement