Advertisement
amonaldo1996

C# -Display .NET Data Providers,databases,tables

Feb 10th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.20 KB | None | 0 0
  1. In this snippet i will show you how to display .NET Data Providers installed on your machine
  2. I will show you how to list all databases and tables foreach one of them
  3.  
  4. /*
  5.  
  6. This snippet is part of an Windows Form Project
  7. It includes the following controls :
  8. comboBox1 : It will list all data providers (access,mysql,oracle,sql server....etc)
  9. comboBox3 : It will list all databases of the selected data provider
  10. comboBox2 : It will all tabes of the selected databases
  11. GridView : that will list the data of the selected table
  12. openFileDialog1 : required to browse for db file with access or sqlite
  13.  
  14. */
  15.  
  16. using System;
  17. using System.Data;
  18. using System.Data.Common;
  19. using System.Windows.Forms;
  20. using System.Data.SqlClient;
  21. namespace ListDataProviders
  22. {
  23.     public partial class Form1 : Form
  24.     {
  25.         String cstr;
  26.         String user, pwd="";
  27.         DataTable db = new DataTable();
  28.         DataTable providers = new DataTable();
  29.         DataTable tables = new DataTable();
  30.         DataTable select;
  31.         DbProviderFactory fact;
  32.         DbConnection cnx;
  33.         DbCommand cmd;
  34.         DbDataReader reader;
  35.         public Form1()
  36.         {
  37.             try
  38.             {
  39.                 InitializeComponent();
  40.                 // SHOW DATA PROVIDERS
  41.                 providers = DbProviderFactories.GetFactoryClasses();
  42.                 foreach (DataRow row in providers.Rows)
  43.                 {
  44.                     if (!comboBox1.Items.Contains(row[2]))
  45.                     {
  46.                         comboBox1.Items.Add(row[2]);
  47.                     }
  48.                 }
  49.             }
  50.             catch (Exception e1)
  51.             {
  52.                 MessageBox.Show(e1.Message);
  53.             }
  54.         }
  55.        
  56.         // when the data provider is changed we will list all its databases
  57.        
  58.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  59.         {
  60.             try
  61.             {
  62.                 comboBox3.Text = "";
  63.                 comboBox3.Items.Clear();
  64.                 fact = DbProviderFactories.GetFactory(comboBox1.SelectedItem.ToString());
  65.                 cnx = fact.CreateConnection();
  66.                 // if it is mysql
  67.                 if (comboBox1.SelectedItem.ToString()=="MySql.Data.MySqlClient")
  68.                 {
  69.                     user = Microsoft.VisualBasic.Interaction.InputBox("Please Input Your User", "User", "");
  70.                     pwd = Microsoft.VisualBasic.Interaction.InputBox("Please Input Your Password", "Password", "");
  71.                     cstr = "Server=localhost;Uid=" + user + ";Pwd=" + pwd+";";
  72.                     cnx.ConnectionString = cstr;
  73.                     cnx.Open();
  74.                     cmd = cnx.CreateCommand();
  75.                     cmd.CommandText = "SHOW DATABASES;";
  76.                     reader = cmd.ExecuteReader();
  77.                     db = new DataTable();
  78.                     db.Load(reader);
  79.                     foreach (DataRow row in db.Rows)
  80.                     {
  81.                         comboBox3.Items.Add(row["Database"]);
  82.                     }
  83.                     cnx.Close();
  84.                 }
  85.                
  86.                 // if it is Oracle then its a special case : we will directly show all tables
  87.                 // because in oracle we have one database per user
  88.                
  89.                 if (comboBox1.SelectedItem.ToString() == "System.Data.OracleClient")
  90.                 {
  91.                     comboBox2.Text = "";
  92.                     comboBox2.Items.Clear();
  93.                     user=Microsoft.VisualBasic.Interaction.InputBox("Please Input Your User","User", "");
  94.                     pwd = Microsoft.VisualBasic.Interaction.InputBox("Please Input Your Password", "Password", "");
  95.                     cstr = "Data Source = localhost; User Id = " + user + "; Password = " + pwd + ";Integrated Security = no;";
  96.                     cnx.ConnectionString = cstr;
  97.                     cnx.Open();
  98.                     cmd = cnx.CreateCommand();
  99.                     cmd.CommandText = "select table_name from user_tables";
  100.                     reader = cmd.ExecuteReader();
  101.                     tables = new DataTable();
  102.                     tables.Load(reader);
  103.                     foreach (DataRow row in tables.Rows)
  104.                     {
  105.                         comboBox2.Items.Add(row[0]);
  106.                     }
  107.                     cnx.Close();  
  108.                 }
  109.                 // if it is access then we will browse for the file
  110.                 if (comboBox1.SelectedItem.ToString()=="System.Data.OleDb")
  111.                 {
  112.                     openFileDialog1.Filter = "Database files (*.mdb, *.accdb)|*.mdb;*.accdb";
  113.                     openFileDialog1.Title = "Choose an access file (accdb,mdb)";
  114.                     if (openFileDialog1.ShowDialog() == DialogResult.OK)
  115.                     {
  116.                         String accdblocation = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
  117.                         comboBox2.Items.Clear();
  118.                         fact = DbProviderFactories.GetFactory(comboBox1.SelectedItem.ToString());
  119.                         cnx = fact.CreateConnection();
  120.                         cstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + accdblocation;
  121.                         cnx.ConnectionString = cstr;
  122.                         cnx.Open();
  123.                         comboBox2.Items.Clear();
  124.                         string[] restrictions = new string[4];
  125.                         restrictions[3] = "Table";
  126.                         tables = new DataTable();
  127.                         tables = cnx.GetSchema("Tables",restrictions);
  128.                         foreach (DataRow row in tables.Rows)
  129.                         {
  130.                             comboBox2.Items.Add(row[2]);
  131.                         }
  132.                     }
  133.                 }
  134.                
  135.                 // if it is sqlite then we will browse for file
  136.                
  137.                 if (comboBox1.SelectedItem.ToString()=="System.Data.SQLite.EF6")
  138.                 {                  
  139.                     openFileDialog1.Filter = "Database files (*.db, *.sqlite)|*.db;*.sqlite";
  140.                     openFileDialog1.Title = "Choose a db file";
  141.                     if (openFileDialog1.ShowDialog() == DialogResult.OK)
  142.                     {
  143.                         cnx.ConnectionString = "Data Source=" + openFileDialog1.InitialDirectory + openFileDialog1.FileName + ";Version=3;";
  144.                         cnx.Open();
  145.                         comboBox2.Items.Clear();
  146.                         tables = new DataTable();
  147.                         tables = cnx.GetSchema("Tables");
  148.                         foreach (DataRow row in tables.Rows)
  149.                         {
  150.                             comboBox2.Items.Add(row[2]);
  151.                         }
  152.                     }
  153.                 }
  154.                 // if it is sql server
  155.                 if (comboBox1.SelectedItem.ToString()=="System.Data.SqlClient")
  156.                 {
  157.                     cnx.ConnectionString = "Server=.\\SQLEXPRESS;Trusted_Connection=True";
  158.                     cnx.Open();
  159.                     cmd = cnx.CreateCommand();
  160.                     cmd.CommandText = "SELECT name FROM sys.databases";
  161.                     reader = cmd.ExecuteReader();
  162.                     db = new DataTable();
  163.                     db.Load(reader);
  164.                     foreach (DataRow row in db.Rows)
  165.                     {
  166.                         comboBox3.Items.Add(row[0]);
  167.                     }
  168.                 }
  169.             }
  170.             catch (Exception ex2)
  171.             {
  172.                 MessageBox.Show(ex2.Message);
  173.             }
  174.         }
  175.        
  176.         // when the database is selected we will list its tables
  177.        
  178.         private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) // when
  179.         {
  180.             try
  181.             {
  182.                 comboBox2.Text = "";
  183.                 comboBox2.Items.Clear();
  184.                 fact = DbProviderFactories.GetFactory(comboBox1.SelectedItem.ToString());
  185.                 cnx = fact.CreateConnection();
  186.                 // if it is mysql
  187.                 if (comboBox1.SelectedItem.ToString()=="MySql.Data.MySqlClient")
  188.                 {
  189.                     cstr = "Server=localhost;Uid=" + user + ";Pwd=" + pwd+";";
  190.                     cnx.ConnectionString = cstr + "Database=" + comboBox3.SelectedItem.ToString() + ";";
  191.                     cnx.Open();
  192.                     tables = new DataTable();
  193.                     tables = cnx.GetSchema("Tables");
  194.                     foreach (DataRow row in tables.Rows)
  195.                     {
  196.                         comboBox2.Items.Add(row[2]);
  197.                     }
  198.                     cnx.Close();
  199.                 }
  200.                 // SI LE SGBD EST SQL SERVER
  201.                 if (comboBox1.SelectedItem.ToString()=="System.Data.SqlClient")
  202.                 {
  203.                     cnx.ConnectionString = "Server=.\\SQLEXPRESS;Trusted_Connection=True;Database="+comboBox3.SelectedItem.ToString();
  204.                     cnx.Open();
  205.                     cmd = cnx.CreateCommand();
  206.                     tables = new DataTable();
  207.                     tables = cnx.GetSchema("Tables");
  208.                     foreach (DataRow row in tables.Rows)
  209.                     {
  210.                         comboBox2.Items.Add(row[2]);
  211.                     }
  212.                     cnx.Close();
  213.                 }
  214.             }
  215.             catch (Exception e3)
  216.             {
  217.                 MessageBox.Show(e3.Message);
  218.             }
  219.         }
  220.        
  221.         // when the table is selected we will show its data
  222.        
  223.         private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
  224.         {
  225.             try
  226.             {
  227.                 fact = DbProviderFactories.GetFactory(comboBox1.SelectedItem.ToString());
  228.                 cnx = fact.CreateConnection();
  229.                 // SI LE SGBD EST MYSQL
  230.                 if (comboBox1.SelectedItem.ToString()=="MySql.Data.MySqlClient")
  231.                 {
  232.                     cstr = "Server=localhost;Uid=" + user + ";Pwd=" + pwd+";";
  233.                     cnx.ConnectionString = cstr + "Database=" + comboBox3.SelectedItem.ToString() + ";";
  234.                     cnx.Open();
  235.                     cmd = cnx.CreateCommand();
  236.                     cmd.CommandText = "SELECT * FROM " + comboBox2.SelectedItem.ToString();
  237.                     reader = cmd.ExecuteReader();
  238.                     select = new DataTable();
  239.                     select.Load(reader);
  240.                     dataGridView1.DataSource = select;
  241.                     cnx.Close();
  242.                 }
  243.                 // SI LE SGBD EST ACCESS
  244.                 if (comboBox1.SelectedItem.ToString()=="System.Data.OleDb")
  245.                 {
  246.                     cstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.InitialDirectory + openFileDialog1.FileName;
  247.                     cnx.ConnectionString = cstr;
  248.                     cnx.Open();
  249.                     cmd = cnx.CreateCommand();
  250.                     cmd.CommandText = "SELECT * FROM " + comboBox2.SelectedItem.ToString();
  251.                     DbDataReader reader = cmd.ExecuteReader();
  252.                     select = new DataTable();
  253.                     select.Load(reader);
  254.                     dataGridView1.DataSource = select;
  255.                     cnx.Close();
  256.                 }
  257.                 // SI LE SGBD EST SQLITE
  258.                 if (comboBox1.SelectedItem.ToString()=="System.Data.SQLite.EF6")
  259.                 {
  260.                     cstr = "Data Source="+openFileDialog1.InitialDirectory + openFileDialog1.FileName + ";Version=3;";
  261.                     cnx.ConnectionString = cstr;
  262.                     cnx.Open();
  263.                     cmd = cnx.CreateCommand();
  264.                     cmd.CommandText = "SELECT * FROM " + comboBox2.SelectedItem.ToString();
  265.                     DbDataReader reader = cmd.ExecuteReader();
  266.                     select = new DataTable();
  267.                     select.Load(reader);
  268.                     dataGridView1.DataSource = select;
  269.                     cnx.Close();
  270.                 }
  271.                 // SI LE SGBD EST SQL SERVER
  272.                 if (comboBox1.SelectedItem.ToString()=="System.Data.SqlClient")
  273.                 {
  274.                     cnx.ConnectionString = "Server=.\\SQLEXPRESS;Trusted_Connection=True;Database=" + comboBox3.SelectedItem.ToString();
  275.                     cnx.Open();
  276.                     cmd = cnx.CreateCommand();
  277.                     cmd.CommandText = "SELECT * FROM " + comboBox2.SelectedItem.ToString();
  278.                     reader = cmd.ExecuteReader();
  279.                     select = new DataTable();
  280.                     select.Load(reader);
  281.                     dataGridView1.DataSource = select;
  282.                     cnx.Close();
  283.                 }
  284.                 // SI LE SGBD EST ORACLE
  285.                 if (comboBox1.SelectedItem.ToString() == "System.Data.OracleClient")
  286.                 {
  287.                     cstr = "Data Source = localhost; User Id = " + user + "; Password = " + pwd + ";Integrated Security = no;";
  288.                     cnx.ConnectionString = cstr;
  289.                     cnx.Open();
  290.                     cmd = cnx.CreateCommand();
  291.                     cmd.CommandText = "SELECT * FROM " + comboBox2.SelectedItem.ToString();
  292.                     reader = cmd.ExecuteReader();
  293.                     select = new DataTable();
  294.                     select.Load(reader);
  295.                     dataGridView1.DataSource = select;
  296.                     cnx.Close();
  297.                 }
  298.             }
  299.             catch (Exception e4)
  300.             {
  301.                 MessageBox.Show(e4.Message);
  302.             }
  303.         }
  304.         /*
  305.         the following events were handled to avoid some exceptions that occured when using keyboard with comboBox
  306.         they are not necessary
  307.         */
  308.         private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  309.         {
  310.             e.Handled = (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down);
  311.         }
  312.         private void comboBox3_KeyDown(object sender, KeyEventArgs e)
  313.         {
  314.             e.Handled = (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down);
  315.         }
  316.         private void comboBox2_KeyDown(object sender, KeyEventArgs e)
  317.         {
  318.             e.Handled = (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down);
  319.         }
  320.     }
  321. }
  322.  
  323. and please don't forget to like our facebook page for funny memes and jokes
  324. http://adf.ly/1jcdRJ
  325. thanxs :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement