Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 KB | None | 0 0
  1. using MySql.Data.MySqlClient;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18.  
  19. namespace DBManagerWin
  20. {
  21.     /// <summary>
  22.     /// Interaction logic for MainWindow.xaml
  23.     /// </summary>
  24.     public partial class MainWindow : Window
  25.     {
  26.         /// Login info variables
  27.         String server = null;
  28.         String database = null;
  29.         String username = null;
  30.         String passwd = null;
  31.         //Database utils
  32.         String connectionInfo = null;
  33.         MySqlDataReader reader = null;
  34.         MySqlConnection connection;
  35.         //////////////////
  36.  
  37.  
  38.  
  39.         public MainWindow()
  40.         {
  41.             InitializeComponent();
  42.             sendQuery.IsEnabled = false;
  43.             queryTxt.IsEnabled = false;
  44.         }
  45.  
  46.         private void onClose(object sender, System.ComponentModel.CancelEventArgs e)
  47.         {
  48.             connection.Close();
  49.         }
  50.  
  51.         private void button_Click(object sender, RoutedEventArgs e)
  52.         {
  53.             server = serverTxt.Text;
  54.             database = databaseTxt.Text;
  55.             username = userTxt.Text;
  56.             passwd = passwdTxt.Text;
  57.  
  58.             if((server.Length < 3)||(database.Length < 1)||(username.Length<1))
  59.             {
  60.                 MessageBox.Show("Fields are not filled correctly!");
  61.                 return;
  62.             }
  63.             connectionInfo = "Data Source = "+server+"; Initial Catalog = "+database+"; User ID = "+username+"; Password = "+passwd+";";
  64.             connection = new MySqlConnection(connectionInfo);
  65.             try { connection.Open(); }
  66.             catch (MySqlException ex){
  67.                 MessageBox.Show("Error - Can't connect to database\nMake sure all fields are filled correctly");
  68.                 return;
  69.             }
  70.  
  71.             MessageBox.Show("Connection good");
  72.             sendQuery.IsEnabled = true;
  73.             queryTxt.IsEnabled = true;
  74.         }
  75.  
  76.         private void sendQueryButton(object sender, RoutedEventArgs e)
  77.         {
  78.             try{
  79.                 String query = queryTxt.Text;
  80.                 if (query.Length >= 6)
  81.                 {
  82.                     if ((query[0] == 'S' || query[0] == 's') && (query[1] == 'e' || query[1] == 'E') && (query[2] == 'l' || query[2] == 'L') && (query[3] == 'e' || query[3] == 'E') && (query[4] == 'C' || query[4] == 'c') && (query[5] == 't' || query[5] == 'T')){
  83.                         showSelect(query);
  84.                         return;
  85.                     }
  86.                 }
  87.  
  88.                 else if(query.Length==0)
  89.                 {
  90.                     MessageBox.Show("Query is empty!");
  91.                     return;
  92.                 }
  93.  
  94.                 MessageBox.Show(query);
  95.                 MySqlCommand mysqlQueryToSend = connection.CreateCommand();
  96.                 mysqlQueryToSend.CommandText = query;
  97.                 mysqlQueryToSend.ExecuteNonQuery();
  98.                 MessageBox.Show("Done!");
  99.             }
  100.             catch(MySqlException ex){
  101.                 MessageBox.Show(ex.Message);
  102.                 return;
  103.             }
  104.             catch(System.NullReferenceException ex){
  105.                 MessageBox.Show(ex.Message);
  106.                 return;
  107.             }
  108.         }
  109.  
  110.         private void showSelect(string query)
  111.         {
  112.             MySqlCommand mysqlQueryToSend = connection.CreateCommand();
  113.             mysqlQueryToSend.CommandText = query;
  114.             reader = mysqlQueryToSend.ExecuteReader();
  115.             MessageBox.Show(reader.FieldCount.ToString());
  116.             DataTable schema = null;
  117.             int rowCount = reader.FieldCount;
  118.             schema = reader.GetSchemaTable();
  119.             string[] rows = new string[rowCount];
  120.             string[] columns = new string[rowCount];
  121.             int index = 0;
  122.             foreach(DataRow x in schema.Rows)
  123.             {
  124.                 rows[index]=x.Field<String>("ColumnName");
  125.                 index++;
  126.             }
  127.             string exit = null;
  128.  
  129.             foreach(var x in rows)
  130.             {
  131.                 exit += " | "+x + " |    ";
  132.             }
  133.  
  134.             exit += "\n";
  135.  
  136.             for (int fi = 0; fi < rowCount; fi++)
  137.             {
  138.                 while (reader.Read())
  139.                 {
  140.                     exit += "\n";
  141.                     foreach (var x in rows)
  142.                     exit += " | "+reader[x].ToString()+" |    ";
  143.                }
  144.            }
  145.  
  146.             resoult.Text = exit;
  147.             reader.Close();
  148.         }
  149.  
  150.     }
  151.    
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement