Advertisement
Ladies_Man

cdb sqlite + mysql conn

Mar 7th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 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.Threading.Tasks;
  7. using System.Data.SQLite;
  8. using MySql.Data;
  9. using MySql.Data.MySqlClient;
  10.  
  11. namespace adonet_4
  12. {
  13.     class Program
  14.     {
  15.         static void exec_sqlite(SQLiteCommand command)
  16.         {
  17.             try
  18.             {
  19.                 SQLiteDataReader reader = command.ExecuteReader();
  20.                 do
  21.                 {
  22.                     while (reader.Read())
  23.                     {
  24.                         Object[] values = new Object[reader.FieldCount];
  25.                         int fieldCount = reader.GetValues(values);
  26.  
  27.                         for (int i = 0; i < fieldCount; i++)
  28.                         {
  29.                             Console.Write("\t{0}", values[i]);
  30.                         }
  31.                         Console.WriteLine();
  32.                     }
  33.  
  34.                 } while (reader.NextResult());
  35.  
  36.                 reader.Close();
  37.  
  38.             }
  39.             catch (Exception e)
  40.             {
  41.                 Console.WriteLine(e.Message);
  42.             }
  43.  
  44.             Console.WriteLine();
  45.         }
  46.  
  47.         static void exec_mysql(MySqlCommand command)
  48.         {
  49.             try
  50.             {
  51.                 MySqlDataReader reader = command.ExecuteReader();
  52.                 do
  53.                 {
  54.                     while (reader.Read())
  55.                     {
  56.                         Object[] values = new Object[reader.FieldCount];
  57.                         int fieldCount = reader.GetValues(values);
  58.  
  59.                         for (int i = 0; i < fieldCount; i++)
  60.                         {
  61.                             Console.Write("\t{0}", values[i]);
  62.                         }
  63.                         Console.WriteLine();
  64.                     }
  65.  
  66.                 } while (reader.NextResult());
  67.  
  68.                 reader.Close();
  69.  
  70.             }
  71.             catch (Exception e)
  72.             {
  73.                 Console.WriteLine(e.Message);
  74.             }
  75.  
  76.             Console.WriteLine();
  77.         }
  78.  
  79.         static void Main(string[] args)
  80.         {
  81.             string conn_string_sqlite = @"Data Source=C:\Users\anthony\Documents\tbmstu.db; Version=3";
  82.             SQLiteConnection conn1 = new SQLiteConnection(conn_string_sqlite);
  83.  
  84.             try
  85.             {
  86.                 conn1.Open();
  87.                 Console.WriteLine("all good. sqlite");
  88.             }
  89.             catch (SQLiteException e)
  90.             {
  91.                 Console.WriteLine(e.Message);
  92.             }
  93.            
  94.  
  95.             SQLiteCommand cmd1 = new SQLiteCommand("select * from persons", conn1);
  96.             exec_sqlite(cmd1);
  97.             conn1.Dispose();
  98.  
  99.  
  100.  
  101.  
  102.             string conn_string_mysql = "Server=localhost;Uid=uid;Pwd=pwd;Database=db1;";
  103.             MySqlConnection conn2 = new MySqlConnection(conn_string_mysql);
  104.  
  105.             try
  106.             {
  107.                 conn2.Open();
  108.                 Console.WriteLine("all good. mysql");
  109.             }
  110.             catch (SQLiteException e)
  111.             {
  112.                 Console.WriteLine(e.Message);
  113.             }
  114.  
  115.             MySqlCommand cmd2 = new MySqlCommand("select * from t", conn2);
  116.             exec_mysql(cmd2);
  117.             conn2.Dispose();
  118.  
  119.  
  120.             Console.ReadLine();
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement