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#
Dlls needed to compile : MEGAUPLOAD - The leading online storage and file delivery service
thanks in advance and sorry for my bad english.
Code:
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SQLite; using MySql.Data; using MySql.Data.MySqlClient; using Npgsql; using NpgsqlTypes; using Npgsql.Design; using System.Net; using System.Security.Cryptography; using System.IO; using System.Drawing; using System.Windows.Forms; /*Basado en MultipleDB de Wilber Torres * Requiere de librerias de MySQL, PostgreSQL y SQLite. * 2008 - ctEngine Inc. todos los derechos reservados. * */ public class multipleDBConnection { public String tipodb; public String server; public String basedatos; public String userdb; public String passwdb; public NpgsqlConnection _connpgsql; public NpgsqlConnection conexionPostgreSQL { set { _connpgsql = value; } get { return _connpgsql; } } public SQLiteConnection consqlite; //public SQLiteDataAdapter dtsqlite; public MySqlConnection conmysql; //public MySqlDataAdapter dtmysql; //public NpgsqlDataAdapter dtnpgsql; public DataSet midataset = new DataSet(); public DataTable midatatable = new DataTable(); public multipleDBConnection(string tipodb, string server, string basedatos, string userdb, string passwdb) { //tipo de servidor this.tipodb = tipodb; //host de servidor (en sqlite es el archivo dentro de la carpeta APP_PATH\Datos\Archivo.ctdb o .sqlite) this.server = server; //base de datos (en caso de mysql o postgre) this.basedatos = basedatos; //datos de login (en caso de mysql o postgre) this.userdb = userdb; this.passwdb = passwdb; } public multipleDBConnection(string tipodb, string server) { this.tipodb = tipodb; this.server = server; } public bool Open() { if (this.tipodb == "sqlite") { this.server = csEngine.getFileFromDatos(this.server); this.consqlite = new SQLiteConnection("Data Source=" + this.server + ";Version=3;New=False;Compress=True;"); try { this.consqlite.Open(); int var1 = 1000, var2 = 0, var3; var3 = var1 / var2; Console.WriteLine(Convert.ToString(var3)); return true; } catch { return false; } } else if (this.tipodb == "mysql") { this.conmysql = new MySqlConnection("Database=" + this.basedatos + ";Data Source=" + this.server + ";User Id=" + this.userdb + ";Password=" + this.passwdb); try { this.conmysql.Open(); return true; } catch { return false; } } else if (this.tipodb == "postgresql") { this._connpgsql = new NpgsqlConnection("Server=" + this.server + ";User Id=" + this.userdb + ";Password=" + this.passwdb + ";Database=" + this.basedatos + ";"); try { this._connpgsql.Open(); return true; } catch { return false; } } else return false; } public Boolean Close() { if (this.tipodb == "sqlite") { this.consqlite.Close(); return true; } else if (this.tipodb == "mysql") { this.conmysql.Close(); return true; } else if (this.tipodb == "postgresql") { conexionPostgreSQL.Close(); return true; } else return false; } } class multipleDBCommand { private String tipo; private multipleDBConnection conexion; //SQLITE private SQLiteCommand sqlitecommand; //MYSQL private MySqlCommand mysqlcommand; //POSTGRE private NpgsqlCommand _postgreCmd; public NpgsqlCommand comandoPostgreSQL { set { _postgreCmd = value; } get { return _postgreCmd; } } public multipleDBCommand(string queryin, multipleDBConnection conexionbd) { switch (conexionbd.tipodb) { case "sqlite": this.sqlitecommand = new SQLiteCommand(queryin, conexionbd.consqlite); break; case "mysql": this.mysqlcommand = new MySqlCommand(queryin, conexionbd.conmysql); break; case "postgresql": comandoPostgreSQL = new NpgsqlCommand(queryin, conexionbd.conexionPostgreSQL); conexionbd._connpgsql.Open(); // CANT MAKE IT WORK... PLEASE HELP?!!! //csEngine.consolaEscribe(Convert.ToString(comandoPostgreSQL.Connection.State)); break; } this.tipo = conexionbd.tipodb; this.conexion = conexionbd; } public multipleDBDataReader ExecuteReader() { switch (this.tipo) { case "sqlite": multipleDBDataReader ejecutaLector = new multipleDBDataReader(sqlitecommand, conexion); return ejecutaLector; case "mysql": multipleDBDataReader ejecutaLectorMy = new multipleDBDataReader(mysqlcommand, conexion); return ejecutaLectorMy; case "postgresql": multipleDBDataReader ejecutaLectorPost = new multipleDBDataReader(comandoPostgreSQL, conexion); return ejecutaLectorPost; default: return null; } } } class multipleDBDataReader { private String tipo; private multipleDBConnection conexionReader; //SQLITE private SQLiteCommand sqlitecommand; private SQLiteDataReader sqlitedatareader; //MYSQL private MySqlCommand mysqlcommand; private MySqlDataReader mysqldatareader; //POSTGRE private NpgsqlCommand postgrecommand; private NpgsqlDataReader postgredatareader; public multipleDBDataReader(SQLiteCommand sqlitecommandin, multipleDBConnection conexionbd) { this.tipo = conexionbd.tipodb; this.conexionReader = conexionbd; this.sqlitecommand = sqlitecommandin; } public multipleDBDataReader(MySqlCommand mysqlcommandin, multipleDBConnection conexionbd) { this.tipo = conexionbd.tipodb; this.conexionReader = conexionbd; this.mysqlcommand = mysqlcommandin; } public multipleDBDataReader(NpgsqlCommand postgrecommandin, multipleDBConnection conexionbd) { this.tipo = conexionbd.tipodb; this.conexionReader = conexionbd; this.postgrecommand = postgrecommandin; } public bool Read() { switch (this.tipo) { case "sqlite": this.tipo = "sqlite"; this.sqlitedatareader = this.sqlitecommand.ExecuteReader(); return sqlitedatareader.Read(); case "mysql": this.tipo = "mysql"; this.mysqldatareader = this.mysqlcommand.ExecuteReader(); return mysqldatareader.Read(); case "postgresql": this.tipo = "postgre"; this.postgredatareader = this.postgrecommand.ExecuteReader(); return postgredatareader.Read(); default: return false; } } public Int16 GetInt16(int columna) { switch (this.tipo) { case "sqlite": this.tipo = "sqlite"; return this.sqlitedatareader.GetInt16(columna); case "mysql": this.tipo = "mysql"; return this.mysqldatareader.GetInt16(columna); case "postgresql": this.tipo = "postgre"; return this.postgredatareader.GetInt16(columna); default: return 0; } } } class pruebita { public static void pruebitax() { //multipleDBConnection conexion = new multipleDBConnection("sqlite", "ctVentas.ctdb"); multipleDBConnection conexion = new multipleDBConnection("postgresql", "localhost", "ctventas", "postgres", "option"); string strQuery = "select * from autobackup limit 1"; multipleDBCommand conectado = new multipleDBCommand(strQuery, conexion); bool conexionabre = conexion.Open(); if (conexionabre) Console.WriteLine("Conexion establecida."); //OK else Console.WriteLine("Conexion imposible"); //ERROR int tiempo = 0, tipo = 0; if (conexion.conexionPostgreSQL.State == ConnectionState.Open) { while (conectado.ExecuteReader().Read()) { tiempo = conectado.ExecuteReader().GetInt16(1); tipo = conectado.ExecuteReader().GetInt16(2); } } //conexion.Close(); Console.WriteLine(Convert.ToString(tiempo) + " " + Convert.ToString(tipo)); } } class csEngine { public static string getFileFromDatos(string file) { string executableName = Application.ExecutablePath; FileInfo executableFileInfo = new FileInfo(executableName); string executableDirectoryName = executableFileInfo.DirectoryName; string backDir = Path.Combine(executableDirectoryName, "Datos"); string resFile = Path.Combine(backDir, file); return resFile; } }
thanks in advance and sorry for my bad english.
Comment