473,416 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 2753
Where does the error occur?
Dec 30 '08 #2

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

Similar topics

1
by: suzy | last post by:
hi, i have created a aspx page template by creating a template.cs file which inherits from the page class. In this file I override the OnInit event and create a template in the form of a...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
2
by: Michel van den Berg | last post by:
Hello, I tried to implement the GoF Singleton Pattern. What do you think? Public MustInherit Class Singleton(Of T As New) Public Sub New() If SingletonCreator.Creating = False Then Throw...
2
by: Mel | last post by:
I have a base font of ..base { font-size: 75%; padding: 10px; } #red { color: red; }
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
3
by: tonydevlin | last post by:
I am creating a workflow in plone using argouml and archgenxml. I have been following the steps on the site:-...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.