Advertisement
Raizen

Untitled

May 12th, 2016
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using NUnit.Framework;
  6. using Selenium;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using Excel = Microsoft.Office.Interop.Excel;
  10. using Access = Microsoft.Office.Interop.Access;
  11. using System.Data.OleDb;
  12. using MySql.Data;
  13. using MySql.Data.MySqlClient;
  14. using System.ComponentModel;
  15. using System.Data;
  16. namespace Lune_Automation_Unite
  17. {
  18.     //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  19.     // ---------- Database_Manager -------------
  20.     // This is the main class of database managment, it handle Access and Excel manipulation
  21.     //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  22.     public class Database_Manager
  23.     {
  24.  
  25.         public Excel.Application process = new Excel.Application();
  26.         public Excel.Workbook database_screen_excel;
  27.         public MySqlConnection sql_connect;
  28.         public DataSet conexaoDataSet;
  29.         public MySqlCommand cmd;
  30.         //public Access.Application process_access = new Access.Application();
  31.         //public Access.AllDataAccessPages database_screen_access;
  32.         //public Excel.Application xle = new Excel.Application();
  33.         //public Excel.Workbook xlev;
  34.  
  35.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  36.         // ---------- Configuração de Variáveis -------------
  37.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  38.  
  39.         public string type;
  40.         public string sql = "";
  41.         public string sqlTable = "";
  42.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  43.         // ---------- Sets the database to Excel -------------
  44.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  45.         public void set_database_excel(string path, string table)
  46.         {
  47.             type = "Excel";
  48.             database_screen_excel = process.Workbooks.Open(path);
  49.             database_screen_excel.Sheets[table].Select();
  50.         }
  51.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  52.         // ---------- Sets the database to SQL -------------
  53.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  54.         public void set_database_mysql(string server, string database, string user, string password)
  55.         {
  56.             type = "MySQL";
  57.             conexaoDataSet = new DataSet();
  58.             sql_connect = new MySqlConnection("Server=" + server + "; Database=" + database + "; Uid=" + user + "; Pwd=" + password);
  59.             sql_connect.Open();
  60.             cmd = new MySqlCommand(sql, sql_connect);
  61.         }
  62.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  63.         // ---------- Sets the database to Access -------------
  64.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  65.         public void set_database_access(string path, string table)
  66.         {
  67.             type = "Access";
  68.             //cria a conexão com o banco de dados
  69.             OleDbConnection aConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:Selenium\teste.mdb");
  70.  
  71.             //cria o objeto command and armazena a consulta SQL
  72.             OleDbCommand aCommand = new OleDbCommand("select * from Tabela1", aConnection);
  73.             aConnection.Open();
  74.             //cria o objeto datareader para fazer a conexao com a tabela
  75.             OleDbDataReader aReader = aCommand.ExecuteReader();
  76.             OleDbDataReader ObjReader = aCommand.ExecuteReader();
  77.             while (aReader.Read())
  78.             {
  79.                 aReader.GetString(1);
  80.             }
  81.         }
  82.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  83.         // ---------- Sets the table -------------
  84.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  85.         public void change_tables(string table)
  86.         // ok!!
  87.         {
  88.             switch (type)
  89.             {
  90.                 case "Excel":
  91.                     database_screen_excel.Sheets[table].Select();
  92.                     break;
  93.                 case "MySQL":
  94.                     sqlTable = table;
  95.                     break;
  96.  
  97.             }
  98.         }
  99.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  100.         // ---------- send_query - Sends a query to the Database -------------
  101.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  102.         public void send_query(string query)
  103.         {
  104.             cmd.CommandText = query;
  105.             try
  106.             {
  107.                 cmd.ExecuteNonQuery();
  108.             }
  109.             catch
  110.             {
  111.                 System.Windows.Forms.MessageBox.Show("Query não executada com sucesso, verificar a Query!");
  112.             }
  113.  
  114.         }
  115.  
  116.         public void write_output(string output){
  117.         }
  118.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  119.         // ---------- Gets the sheet Name -------------
  120.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  121.         public string activeSheetName()
  122.         {
  123.             return database_screen_excel.ActiveSheet.Name.ToString();
  124.         }
  125.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  126.         // ---------- Writes in the Database (SQL) -------------
  127.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  128.         public void write_new_output(string output, string table, string fields, string values)
  129.         {
  130.             cmd.CommandText = "INSERT INTO " + table + " (" + fields + "); VALUES (" + values + ");";
  131.  
  132.             cmd.ExecuteNonQuery();
  133.         }
  134.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  135.         // ---------- Writes in the Database (Excel) -------------
  136.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  137.         public void write_output(string output, int row, int col)
  138.         {
  139.             database_screen_excel.Application.Cells[row, col].Value = output;
  140.         }
  141.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  142.         // ---------- Retreives data from the Database -------------
  143.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  144.         public string get_data(int row, int col)
  145.         {
  146.             if (database_screen_excel.Application.Cells[row, col].Value == null)
  147.                 return "";
  148.             else
  149.                 return database_screen_excel.Application.Cells[row, col].Value.ToString();
  150.         }
  151.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  152.         // ---------- Closes connection to the database -------------
  153.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  154.         public void close_database()
  155.         {
  156.             switch (type)
  157.             {
  158.                 case "Excel":
  159.                     database_screen_excel.Save();
  160.                     database_screen_excel.Close();
  161.                     break;
  162.             }
  163.         }
  164.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  165.         // ---------- Saves Database Changes -------------
  166.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  167.         internal void save_database()
  168.         {
  169.             database_screen_excel.Save();
  170.         }
  171.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  172.         // ---------- Adds a new row to the database -------------
  173.         //=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=//
  174.         internal void addRow(int testCaserow)
  175.         {
  176.             database_screen_excel.Application.Cells[testCaserow, 1].EntireRow.Insert();
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement