472,103 Members | 1,032 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,103 software developers and data experts.

PLEASE HELP ON THIS... Creating a class...

Hi, im new to csharp and im trying to create a class that can change the application database without no rewriting all connection code... but cause some reason it is not working... it tells me that im not creating the object but im doing it,,, please help im a newbie to c#

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SQLite;
  6. using MySql.Data;
  7. using MySql.Data.MySqlClient;
  8. using Npgsql;
  9. using NpgsqlTypes;
  10. using Npgsql.Design;
  11. using System.Net;
  12. using System.Security.Cryptography;
  13. using System.IO;
  14. using System.Drawing;
  15. using System.Windows.Forms;
  16.  
  17. /*Basado en MultipleDB de Wilber Torres
  18.  * Requiere de librerias de MySQL, PostgreSQL y SQLite.
  19.  * 2008 - ctEngine Inc. todos los derechos reservados.
  20.  * */
  21. public class multipleDBConnection
  22. {
  23.     public String tipodb;
  24.  
  25.     public String server;
  26.     public String basedatos;
  27.     public String userdb;
  28.     public String passwdb;
  29.  
  30.     public NpgsqlConnection _connpgsql;
  31.  
  32.     public NpgsqlConnection conexionPostgreSQL
  33.     {
  34.         set { _connpgsql = value; }
  35.         get { return _connpgsql; }
  36.     }
  37.  
  38.     public SQLiteConnection consqlite;
  39.     //public SQLiteDataAdapter dtsqlite;
  40.  
  41.     public MySqlConnection conmysql;
  42.     //public MySqlDataAdapter dtmysql;
  43.  
  44.  
  45.     //public NpgsqlDataAdapter dtnpgsql;
  46.  
  47.     public DataSet midataset = new DataSet();
  48.     public DataTable midatatable = new DataTable();
  49.  
  50.  
  51.  
  52.     public multipleDBConnection(string tipodb, string server, string basedatos, string userdb, string passwdb)
  53.     {
  54.         //tipo de servidor
  55.         this.tipodb = tipodb;
  56.         //host de servidor (en sqlite es el archivo dentro de la carpeta APP_PATH\Datos\Archivo.ctdb o .sqlite)
  57.         this.server = server;
  58.         //base de datos (en caso de mysql o postgre)
  59.         this.basedatos = basedatos;
  60.         //datos de login (en caso de mysql o postgre)
  61.         this.userdb = userdb;
  62.         this.passwdb = passwdb;
  63.     }
  64.     public multipleDBConnection(string tipodb, string server)
  65.     {
  66.         this.tipodb = tipodb;
  67.         this.server = server;
  68.     }
  69.  
  70.     public bool Open()
  71.     {
  72.         if (this.tipodb == "sqlite")
  73.         {
  74.             this.server = csEngine.getFileFromDatos(this.server);
  75.  
  76.             this.consqlite = new SQLiteConnection("Data Source=" + this.server + ";Version=3;New=False;Compress=True;");
  77.             try
  78.             {
  79.                 this.consqlite.Open();
  80.                 int var1 = 1000, var2 = 0, var3;
  81.                 var3 = var1 / var2;
  82.                 Console.WriteLine(Convert.ToString(var3));
  83.                 return true;
  84.             }
  85.             catch
  86.             {
  87.                 return false;
  88.             }
  89.         }
  90.         else if (this.tipodb == "mysql")
  91.         {
  92.             this.conmysql = new MySqlConnection("Database=" + this.basedatos + ";Data Source=" + this.server + ";User Id=" + this.userdb + ";Password=" + this.passwdb);
  93.             try
  94.             {
  95.                 this.conmysql.Open();
  96.                 return true;
  97.             }
  98.             catch
  99.             {
  100.                 return false;
  101.             }
  102.  
  103.         }
  104.         else if (this.tipodb == "postgresql")
  105.         {
  106.             this._connpgsql = new NpgsqlConnection("Server=" + this.server + ";User Id=" + this.userdb + ";Password=" + this.passwdb + ";Database=" + this.basedatos + ";");
  107.             try
  108.             {
  109.                 this._connpgsql.Open();
  110.                 return true;
  111.             }
  112.             catch
  113.             {
  114.                 return false;
  115.             }
  116.         }
  117.         else
  118.             return false;
  119.  
  120.     }
  121.  
  122.     public Boolean Close()
  123.     {
  124.         if (this.tipodb == "sqlite")
  125.         {
  126.             this.consqlite.Close();
  127.             return true;
  128.         }
  129.         else if (this.tipodb == "mysql")
  130.         {
  131.             this.conmysql.Close();
  132.             return true;
  133.         }
  134.         else if (this.tipodb == "postgresql")
  135.         {
  136.             conexionPostgreSQL.Close();
  137.             return true;
  138.         }
  139.         else
  140.             return false;
  141.     }
  142.  
  143.  
  144. }
  145. class multipleDBCommand
  146. {
  147.  
  148.     private String tipo;
  149.     private multipleDBConnection conexion;
  150.     //SQLITE
  151.     private SQLiteCommand sqlitecommand;
  152.     //MYSQL
  153.     private MySqlCommand mysqlcommand;
  154.     //POSTGRE
  155.  
  156.     private NpgsqlCommand _postgreCmd;
  157.     public NpgsqlCommand comandoPostgreSQL
  158.     {
  159.         set { _postgreCmd = value; }
  160.         get { return _postgreCmd; }
  161.     }
  162.  
  163.     public multipleDBCommand(string queryin, multipleDBConnection conexionbd)
  164.     {
  165.         switch (conexionbd.tipodb)
  166.         {
  167.             case "sqlite":
  168.                 this.sqlitecommand = new SQLiteCommand(queryin, conexionbd.consqlite);
  169.                 break;
  170.             case "mysql":
  171.                 this.mysqlcommand = new MySqlCommand(queryin, conexionbd.conmysql);
  172.                 break;
  173.             case "postgresql":
  174.                 comandoPostgreSQL = new NpgsqlCommand(queryin, conexionbd.conexionPostgreSQL);
  175.                 conexionbd._connpgsql.Open(); // CANT MAKE IT WORK... PLEASE HELP?!!!
  176.                 //csEngine.consolaEscribe(Convert.ToString(comandoPostgreSQL.Connection.State));
  177.                 break;
  178.         }
  179.         this.tipo = conexionbd.tipodb;
  180.         this.conexion = conexionbd;
  181.     }
  182.  
  183.     public multipleDBDataReader ExecuteReader()
  184.     {
  185.         switch (this.tipo)
  186.         {
  187.             case "sqlite":
  188.                 multipleDBDataReader ejecutaLector = new multipleDBDataReader(sqlitecommand, conexion);
  189.                 return ejecutaLector;
  190.             case "mysql":
  191.                 multipleDBDataReader ejecutaLectorMy = new multipleDBDataReader(mysqlcommand, conexion);
  192.                 return ejecutaLectorMy;
  193.             case "postgresql":
  194.                 multipleDBDataReader ejecutaLectorPost = new multipleDBDataReader(comandoPostgreSQL, conexion);
  195.                 return ejecutaLectorPost;
  196.             default:
  197.                 return null;
  198.         }
  199.     }
  200.  
  201. }
  202. class multipleDBDataReader
  203. {
  204.  
  205.     private String tipo;
  206.     private multipleDBConnection conexionReader;
  207.     //SQLITE
  208.     private SQLiteCommand sqlitecommand;
  209.     private SQLiteDataReader sqlitedatareader;
  210.     //MYSQL
  211.     private MySqlCommand mysqlcommand;
  212.     private MySqlDataReader mysqldatareader;
  213.     //POSTGRE
  214.     private NpgsqlCommand postgrecommand;
  215.     private NpgsqlDataReader postgredatareader;
  216.  
  217.     public multipleDBDataReader(SQLiteCommand sqlitecommandin, multipleDBConnection conexionbd)
  218.     {
  219.         this.tipo = conexionbd.tipodb;
  220.         this.conexionReader = conexionbd;
  221.         this.sqlitecommand = sqlitecommandin;
  222.     }
  223.  
  224.     public multipleDBDataReader(MySqlCommand mysqlcommandin, multipleDBConnection conexionbd)
  225.     {
  226.         this.tipo = conexionbd.tipodb;
  227.         this.conexionReader = conexionbd;
  228.         this.mysqlcommand = mysqlcommandin;
  229.     }
  230.     public multipleDBDataReader(NpgsqlCommand postgrecommandin, multipleDBConnection conexionbd)
  231.     {
  232.         this.tipo = conexionbd.tipodb;
  233.         this.conexionReader = conexionbd;
  234.         this.postgrecommand = postgrecommandin;
  235.     }
  236.  
  237.     public bool Read()
  238.     {
  239.         switch (this.tipo)
  240.         {
  241.             case "sqlite":
  242.                 this.tipo = "sqlite";
  243.  
  244.                 this.sqlitedatareader = this.sqlitecommand.ExecuteReader();
  245.  
  246.                 return sqlitedatareader.Read();
  247.             case "mysql":
  248.                 this.tipo = "mysql";
  249.                 this.mysqldatareader = this.mysqlcommand.ExecuteReader();
  250.                 return mysqldatareader.Read();
  251.             case "postgresql":
  252.                 this.tipo = "postgre";
  253.                 this.postgredatareader = this.postgrecommand.ExecuteReader();
  254.                 return postgredatareader.Read();
  255.             default:
  256.                 return false;
  257.         }
  258.     }
  259.     public Int16 GetInt16(int columna)
  260.     {
  261.         switch (this.tipo)
  262.         {
  263.             case "sqlite":
  264.                 this.tipo = "sqlite";
  265.                 return this.sqlitedatareader.GetInt16(columna);
  266.  
  267.             case "mysql":
  268.                 this.tipo = "mysql";
  269.                 return this.mysqldatareader.GetInt16(columna);
  270.  
  271.             case "postgresql":
  272.                 this.tipo = "postgre";
  273.                 return this.postgredatareader.GetInt16(columna);
  274.             default:
  275.                 return 0;
  276.         }
  277.     }
  278. }
  279.  
  280. class pruebita
  281. {
  282.     public static void pruebitax()
  283.     {
  284.         //multipleDBConnection conexion = new multipleDBConnection("sqlite", "ctVentas.ctdb");
  285.         multipleDBConnection conexion = new multipleDBConnection("postgresql", "localhost", "ctventas", "postgres", "option");
  286.         string strQuery = "select * from autobackup limit 1";
  287.         multipleDBCommand conectado = new multipleDBCommand(strQuery, conexion);
  288.         bool conexionabre = conexion.Open();
  289.         if (conexionabre)
  290.             Console.WriteLine("Conexion establecida."); //OK
  291.         else
  292.             Console.WriteLine("Conexion imposible"); //ERROR
  293.         int tiempo = 0, tipo = 0;
  294.         if (conexion.conexionPostgreSQL.State == ConnectionState.Open)
  295.         {
  296.             while (conectado.ExecuteReader().Read())
  297.             {
  298.                 tiempo = conectado.ExecuteReader().GetInt16(1);
  299.                 tipo = conectado.ExecuteReader().GetInt16(2);
  300.             }
  301.         }
  302.         //conexion.Close();
  303.         Console.WriteLine(Convert.ToString(tiempo) + " " + Convert.ToString(tipo));
  304.     }
  305. }
  306. class csEngine
  307. {
  308.     public static string getFileFromDatos(string file)
  309.     {
  310.         string executableName = Application.ExecutablePath;
  311.         FileInfo executableFileInfo = new FileInfo(executableName);
  312.         string executableDirectoryName = executableFileInfo.DirectoryName;
  313.         string backDir = Path.Combine(executableDirectoryName, "Datos");
  314.         string resFile = Path.Combine(backDir, file);
  315.         return resFile;
  316.     }
  317.  
  318. }
Dlls needed to compile : MEGAUPLOAD - The leading online storage and file delivery service

thanks in advance and sorry for my bad english.
Dec 28 '08 #1
1 2584
Where does the error occur?
Dec 30 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by suzy | last post: by
5 posts views Thread by Just Me | last post: by
2 posts views Thread by Michel van den Berg | last post: by
2 posts views Thread by Mel | last post: by
4 posts views Thread by Tarun Mistry | last post: by
3 posts views Thread by tonydevlin | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.