Foros del Web » Programación para mayores de 30 ;) » .NET »

Conversion CS a VB

Estas en el tema de Conversion CS a VB en el foro de .NET en Foros del Web. Hola miagos del foro he descargado un proyecto para manejo de dase de datos maestro detalle y esta escrito en CS bueno he logrado traducir ...
  #1 (permalink)  
Antiguo 26/05/2008, 12:39
 
Fecha de Ingreso: octubre-2003
Ubicación: La Paz - Bolivia
Mensajes: 116
Antigüedad: 20 años, 7 meses
Puntos: 1
Conversion CS a VB

Hola miagos del foro he descargado un proyecto para manejo de dase de datos maestro detalle y esta escrito en CS bueno he logrado traducir todo el codigo a VB pero me da error y no me corre el programa, aqui les dejo el codigo original y traducido

este codigo es en CSharp [original]
Código:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Common.Factory;
namespace DataAccessLayer
{
    abstract public class DataObjectBase: IDataObject
    {
        private IDbTransaction trans = null;
        private IDbConnection connection = null;
        private string currentConnection = String.Empty;
        private DataTable dataTable = null;
        private DataRow newRow = null;
        private bool bCloseExternalConnectionAfterUse = false;
        private string conditionClause = String.Empty;
        public string commandTextSelect = string.Empty;
        public string commandTextInsert = string.Empty;
        public string commandTextUpdate = string.Empty;
        public string commandTextDeleteByKey = string.Empty;
        public string commandTextDeleteAll = string.Empty;
 
        #region "CONSTRUCTORS"
 
  public DataObjectBase()
  {
            currentConnection = Shared.SharedVariables.ConnectionString;
            ConfigureDataObject();
  }
 
  public DataObjectBase(string connectionString)
  {
   connectionString=ConnectionString;
            ConfigureDataObject();
  }
        public DataObjectBase(string connectionString, string condition)
  {
            currentConnection = connectionString;
            conditionClause = condition;
            ConfigureDataObject();
  }
 
  public DataObjectBase(IDbConnection externalConnection)
  {
            connection = externalConnection;
            ConfigureDataObject();
  }
        public DataObjectBase(IDbConnection externalConnection, string condition)
  {
            connection = externalConnection;
            conditionClause = condition;
            ConfigureDataObject();
  }
 
  #endregion
 
  #region "PROPERTIES"
        public IDbTransaction Transaction
  {
   get
   {
    return trans;
   }
   set
   {
    if (trans == value)
    {
     return;
    }
    trans = value;
   }
  }
 
  public IDbConnection Connection
  {
   get
   {
    return connection;
   }
   set
   {
    if (connection == value)
    {
     return;
    }
    connection = value;
                ConfigureDataObject();
   }
  }
 
  public string ConnectionString
  {
   get
   {
    return currentConnection;
   }
   set
   {
    if (currentConnection == value)
    {
     return;
    }
    currentConnection = value;
   }
  }
 
  public string Condition
  {
   get
   {
    return conditionClause;
   }
   set
   {
    if (conditionClause == value)
    {
     return;
    }
    conditionClause = value;
   }
  }
 
  public DataTable DataTable
  {
   get
   {
    dataTable=GetDataTable();
    return dataTable;
   }
  }
 
  public bool CloseExternalConnectionAfterUse
        {
            get
            {
                return bCloseExternalConnectionAfterUse;
            }
            set
            {
                if (bCloseExternalConnectionAfterUse == value)
                    return;
                bCloseExternalConnectionAfterUse = value;
            }
        }
 
  #endregion
  #region "CRUID METHODS"
 
        public int InsertData(DataRow row)
        {
            this.OpenConnection();
            int iResult = 0;
            IDbCommand cmdInsert = DataFactory.CreateCommand();
            cmdInsert.CommandText = commandTextInsert;
            cmdInsert.CommandType = Shared.SharedVariables.DBCommandType;
            cmdInsert.Connection = this.connection;
            if (this.trans != null)
            {
                cmdInsert.Transaction = trans;
            }
 
            ConfigureParameterValue(ref cmdInsert, ref row);
 
            try
            {
                iResult = cmdInsert.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
                if (cmdInsert != null)
                {
                    cmdInsert.Dispose();
                    cmdInsert = null;
                }
            }
            return iResult;
        }
        public int UpdateData(DataRow row)
        {
            this.OpenConnection();
            int retVal = 0;
            IDbCommand cmdUpdate  = DataFactory.CreateCommand();
            cmdUpdate.CommandText = commandTextUpdate;
            cmdUpdate.CommandType = Shared.SharedVariables.DBCommandType;
            cmdUpdate.Connection = this.connection;
            if (this.trans != null)
            {
                cmdUpdate.Transaction = trans;
            }
            ConfigureParameterValue(ref cmdUpdate, ref row);
            try
            {
                retVal = cmdUpdate.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
                if (cmdUpdate != null)
                {
                    cmdUpdate.Dispose();
                    cmdUpdate = null;
                }
            }
            return retVal;
        }
        public int DeleteByKey(string strKey)
        {
            int retVal = 0;
            try
            {
                retVal = DeleteAll(commandTextDeleteByKey + strKey);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return retVal;
        }
        public int DeleteAll(string condition)
        {
            int retVal = 0;
            this.OpenConnection();
            IDbCommand cmdDelete;
            cmdDelete = DataFactory.CreateCommand();
            cmdDelete.CommandText = commandTextDeleteAll +
                                    condition;
            cmdDelete.Connection = connection;
            if (trans != null) cmdDelete.Transaction = trans;
            try
            {
                retVal = cmdDelete.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                this.CloseConnection();
                if (cmdDelete != null)
                {
                    cmdDelete.Dispose();
                    cmdDelete = null;
                }
            }
            return retVal;
        }
 
        #endregion
        #region "ABSTRACT CONFIGURATION"
        abstract protected void ConfigureParameterValue(ref IDbCommand cmd,
                                                ref DataRow row);
        abstract protected void ConfigureDataObject();
        #endregion
        #region "OTHERS METHODS"
        public DataTable GetDataTable()
  {
   this.OpenConnection();
 
   IDataAdapter da = DataFactory.CreateDataAdapter(commandTextSelect + this.conditionClause,
                                                        connection);
   DataTable dtTemp = null;
            DataSet dsTemp = null;
 
   try
   {
                dsTemp = new DataSet();
    dtTemp = new DataTable();
 
                da.Fill(dsTemp);
                if (dsTemp.Tables.Count != 0) dtTemp = dsTemp.Tables[0];
    newRow=dtTemp.NewRow();
   }
   catch (Exception ex)
   {
    throw ex;
   }
   finally
   {
    // Clear the memory used by the command
    this.CloseConnection();
    da = null;
   }
 
   return dtTemp;
 
  }
        public int GetRecordCount()
        {
            int iResult = 0;
            this.OpenConnection();
            IDbCommand cmd = DataFactory.CreateCommand();
            cmd.CommandText=commandTextSelect + this.conditionClause;
            cmd.Connection = connection;
            try
            {
                iResult = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                // Clear the memory used by the command
                this.CloseConnection();
                cmd = null;
            }
            return iResult;
        }
        protected DataRow GetNewRowFormat()
  {
   this.OpenConnection();
 
   IDataAdapter da = DataFactory.CreateDataAdapter(commandTextSelect + " WHERE 1=0", connection);
            DataTable dtTemp = null;
            DataSet dsTemp = null;
 
   try
   {
                dsTemp = new DataSet();
                dtTemp = new DataTable();
                da.Fill(dsTemp);
                if (dsTemp.Tables.Count != 0) dtTemp = dsTemp.Tables[0];
                newRow = dtTemp.NewRow();
   }
   catch (Exception ex)
   {
    throw ex;
   }
   finally
   {
    // Clear the memory used by the command
    this.CloseConnection();
 
    da = null;
   }
            return dtTemp.NewRow();
  }
 
  public void OpenConnection()
  {
   if(currentConnection != "")
   {
                if (connection == null) connection = DataFactory.CreateConnection();
                if (connection.State == ConnectionState.Closed)
                {
                    connection.ConnectionString = this.currentConnection;
                    connection.Open();
                }
   }
   else
   {
    if(connection.State==ConnectionState.Closed)
    {
     connection.Open();
     bCloseExternalConnectionAfterUse=true;
    }
   }
  }
 
  public void CloseConnection()
  {
   if(currentConnection != "")
   {
    if(connection!=null)
    {
     connection.Dispose();
     connection=null;
    }
   }
   else
   {
    if(bCloseExternalConnectionAfterUse) connection.Close();
   }
  }
 
  public DataRow NewRow()
  {
   if(newRow==null) newRow=GetNewRowFormat();
   return newRow;
  }
  #endregion
    }
}
__________________
"El viento puede soplar fuerte, pero la montaña no lo reverencia"
  #2 (permalink)  
Antiguo 26/05/2008, 12:41
 
Fecha de Ingreso: octubre-2003
Ubicación: La Paz - Bolivia
Mensajes: 116
Antigüedad: 20 años, 7 meses
Puntos: 1
Conversion CS a VB [continuacion]

Codigo tradicido en VB
Código:
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports Common.Common.Factory
Imports SharedComponents.SharedComponents
Namespace DataAccessLayer
Public MustInherit Class DataObjectBase
Implements IDataObject
 
Private trans As IDbTransaction = Nothing
Private connection As IDbConnection = Nothing
Private currentConnection As String = String.Empty
Private dataTable As DataTable = Nothing
Private newRow As DataRow = Nothing
Private bCloseExternalConnectionAfterUse As Boolean = False
Private conditionClause As String = String.Empty
Public commandTextSelect As String = String.Empty
Public commandTextInsert As String = String.Empty
Public commandTextUpdate As String = String.Empty
Public commandTextDeleteByKey As String = String.Empty
Public commandTextDeleteAll As String = String.Empty
 
#Region "CONSTRUCTORS"
Public Sub New()
currentConnection = SharedVariables.ConnectionString
ConfigureDataObject()
End Sub
Public Sub New(ByVal connectionString As String)
connectionString = connectionString
ConfigureDataObject()
End Sub
Public Sub New(ByVal connectionString As String, ByVal condition As String)
currentConnection = connectionString
conditionClause = condition
ConfigureDataObject()
End Sub
Public Sub New(ByVal externalConnection As IDbConnection)
connection = externalConnection
ConfigureDataObject()
End Sub
Public Sub New(ByVal externalConnection As IDbConnection, ByVal condition As String)
connection = externalConnection
conditionClause = condition
ConfigureDataObject()
End Sub
#End Region
#Region "PROPERTIES"
Public Property Transaction() As IDbTransaction Implements IDataObject.Transaction
Get
Return trans
End Get
Set(ByVal value As IDbTransaction)
If trans Is value Then
Return
End If
trans = value
End Set
End Property
Public Property Connection() As IDbConnection Implements IDataObject.Connections
Get
Return Connection
End Get
Set(ByVal value As IDbConnection)
If connection Is value Then
Return
End If
connection = value
ConfigureDataObject()
End Set
End Property
Public Property ConnectionString() As String Implements IDataObject.ConnectionString
Get
Return currentConnection
End Get
Set(ByVal value As String)
If currentConnection = value Then
Return
End If
currentConnection = value
End Set
End Property
Public Property Condition() As String Implements IDataObject.Condition
Get
Return conditionClause
End Get
Set(ByVal value As String)
If conditionClause = value Then
Return
End If
conditionClause = value
End Set
End Property
Public Property DataTable() As DataTable Implements IDataObject.DataTable
Get
DataTable = GetDataTable()
Return DataTable
End Get
Set(ByVal value As DataTable)
End Set
End Property
Public Property CloseExternalConnectionAfterUse() As Boolean Implements IDataObject.CloseExternalConnectionAfterUse
Get
Return bCloseExternalConnectionAfterUse
End Get
Set(ByVal value As Boolean)
If bCloseExternalConnectionAfterUse = value Then
Return
End If
bCloseExternalConnectionAfterUse = value
End Set
End Property
 
#End Region
#Region "CRUID METHODS"
 
Public Function InsertData(ByVal row As DataRow) As Integer Implements IDataObject.InsertData
Me.OpenConnection()
Dim iResult As Integer = 0
Dim cmdInsert As IDbCommand = DataFactory.CreateCommand()
cmdInsert.CommandText = commandTextInsert
cmdInsert.CommandType = SharedVariables.DBCommandType
cmdInsert.Connection = Me.connection
If Not Me.trans Is Nothing Then
cmdInsert.Transaction = trans
End If
 
ConfigureParameterValue(cmdInsert, row)
 
Try
iResult = cmdInsert.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
Me.CloseConnection()
If Not cmdInsert Is Nothing Then
cmdInsert.Dispose()
cmdInsert = Nothing
End If
End Try
Return iResult
End Function
Public Function UpdateData(ByVal row As DataRow) As Integer Implements IDataObject.UpdateData
Me.OpenConnection()
Dim retVal As Integer = 0
Dim cmdUpdate As IDbCommand = DataFactory.CreateCommand()
cmdUpdate.CommandText = commandTextUpdate
cmdUpdate.CommandType = SharedVariables.DBCommandType
cmdUpdate.Connection = Me.connection
If Not Me.trans Is Nothing Then
cmdUpdate.Transaction = trans
End If
ConfigureParameterValue(cmdUpdate, row)
Try
retVal = cmdUpdate.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
Me.CloseConnection()
If Not cmdUpdate Is Nothing Then
cmdUpdate.Dispose()
cmdUpdate = Nothing
End If
End Try
Return retVal
End Function
Public Function DeleteByKey(ByVal strKey As String) As Integer Implements IDataObject.DeleteByKey
Dim retVal As Integer = 0
Try
retVal = DeleteAll(commandTextDeleteByKey & strKey)
Catch ex As Exception
Throw ex
Finally
End Try
Return retVal
End Function
Public Function DeleteAll(ByVal condition As String) As Integer Implements IDataObject.DeleteAll
Dim retVal As Integer = 0
Me.OpenConnection()
Dim cmdDelete As IDbCommand
cmdDelete = DataFactory.CreateCommand()
cmdDelete.CommandText = commandTextDeleteAll & condition
cmdDelete.Connection = connection
If Not trans Is Nothing Then cmdDelete.Transaction = trans
Try
retVal = cmdDelete.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
Me.CloseConnection()
If Not cmdDelete Is Nothing Then
cmdDelete.Dispose()
cmdDelete = Nothing
End If
End Try
Return retVal
End Function
 
#End Region
__________________
"El viento puede soplar fuerte, pero la montaña no lo reverencia"

Última edición por Gusys; 26/05/2008 a las 12:43 Razón: etiquetas code
  #3 (permalink)  
Antiguo 26/05/2008, 12:42
 
Fecha de Ingreso: octubre-2003
Ubicación: La Paz - Bolivia
Mensajes: 116
Antigüedad: 20 años, 7 meses
Puntos: 1
Conversion CS a VB [continuacion I]

Código:
#Region "ABSTRACT CONFIGURATION"
Protected Overridable Sub ConfigureParameterValue(ByRef cmd As IDbCommand, ByRef row As DataRow)
End Sub
Protected Overridable Sub ConfigureDataObject()
End Sub
#End Region
#Region "OTHERS METHODS"
Public Function GetDataTable() As DataTable Implements IDataObject.GetDataTable
Me.OpenConnection()
Dim da As IDataAdapter = DataFactory.CreateDataAdapter(commandTextSelect & Me.conditionClause, connection)
Dim dtTemp As DataTable = Nothing
Dim dsTemp As DataSet = Nothing
Try
dsTemp = New DataSet()
dtTemp = New DataTable()
da.Fill(dsTemp)
If dsTemp.Tables.Count <> 0 Then dtTemp = dsTemp.Tables(0)
Me.newRow = dtTemp.NewRow()
Catch ex As Exception
Throw ex
Finally
'Clear the memory used by the command
Me.CloseConnection()
da = Nothing
End Try
Return dtTemp
End Function
Public Function GetRecordCount() As Integer Implements IDataObject.GetRecordCount
Dim iResult As Integer = 0
Me.OpenConnection()
Dim cmd As IDbCommand = DataFactory.CreateCommand()
cmd.CommandText = commandTextSelect + Me.conditionClause
cmd.Connection = connection
Try
iResult = cmd.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
' Clear the memory used by the command
Me.CloseConnection()
cmd = Nothing
End Try
Return iResult
End Function
Protected Function GetNewRowFormat() As DataRow
Me.OpenConnection()
Dim da As IDataAdapter = DataFactory.CreateDataAdapter(commandTextSelect & " WHERE 1=0", connection)
Dim dtTemp As DataTable = Nothing
Dim dsTemp As DataSet = Nothing
Try
dsTemp = New DataSet()
dtTemp = New DataTable()
da.Fill(dsTemp)
If dsTemp.Tables.Count <> 0 Then dtTemp = dsTemp.Tables(0)
Me.newRow = dtTemp.NewRow()
Catch ex As Exception
Throw ex
Finally
'Clear the memory used by the command
Me.CloseConnection()
da = Nothing
End Try
Return dtTemp.NewRow()
End Function
Public Sub OpenConnection()
If currentConnection <> "" Then
If connection Is Nothing Then connection = DataFactory.CreateConnection()
If connection.State = ConnectionState.Closed Then
connection.ConnectionString = Me.currentConnection
connection.Open()
End If
Else
If connection.State = ConnectionState.Closed Then
connection.Open()
bCloseExternalConnectionAfterUse = True
End If
End If
End Sub
 
Public Sub CloseConnection() Implements IDataObject.CloseConnection
If currentConnection <> "" Then
If Not connection Is Nothing Then
connection.Dispose()
connection = Nothing
End If
Else
If bCloseExternalConnectionAfterUse Then connection.Close()
End If
End Sub
Public Function NewRow() As DataRow Implements IDataObject.NewRow
If NewRow Is Nothing Then NewRow = GetNewRowFormat()
Return NewRow
End Function
#End Region
End Class
End Namespace
ahora me da error en la funcion NewRow, DataTable y Connection

Aqui les dejo los proyectos
Original CSharp
http://www.sendspace.com/file/ewwf1j
Traducido a VB
http://www.sendspace.com/file/bsotim

denme sus opiniones si con este proyecto es la menjor forma de manejar base de datos o existe una mejor bueno no soy experto en Vstudio pero toy empezando de antemano muchas gracias
__________________
"El viento puede soplar fuerte, pero la montaña no lo reverencia"

Última edición por Gusys; 26/05/2008 a las 12:43 Razón: etiqueta code
  #4 (permalink)  
Antiguo 26/05/2008, 15:00
Avatar de RootK
Moderador
 
Fecha de Ingreso: febrero-2002
Ubicación: México D.F
Mensajes: 8.004
Antigüedad: 22 años, 3 meses
Puntos: 50
Respuesta: Conversion CS a VB

Cita:
Iniciado por Gusys
si con este proyecto es la menjor forma de manejar base de datos o existe una mejor bueno no soy experto en Vstudio pero toy empezando de antemano muchas gracias
Lo mejor es usar los Enterprise Libraries para el acceso a datos

Salu2
__________________
Nadie roba nada ya que en la vida todo se paga . . .

Exentrit - Soluciones SharePoint & Net
  #5 (permalink)  
Antiguo 27/05/2008, 09:12
 
Fecha de Ingreso: octubre-2003
Ubicación: La Paz - Bolivia
Mensajes: 116
Antigüedad: 20 años, 7 meses
Puntos: 1
Respuesta: Conversion CS a VB

OK, muchas gracias por la sugerencia, existe algun manual para utilizar esas librerias, dunciona con MySQL, hay que instalar algo adicional para que funcione con MySQL, tengo instalado el connector de MySQL...

gracias otra ves
__________________
"El viento puede soplar fuerte, pero la montaña no lo reverencia"
  #6 (permalink)  
Antiguo 27/05/2008, 13:13
Avatar de RootK
Moderador
 
Fecha de Ingreso: febrero-2002
Ubicación: México D.F
Mensajes: 8.004
Antigüedad: 22 años, 3 meses
Puntos: 50
Respuesta: Conversion CS a VB

Cita:
dunciona con MySQL, hay que instalar algo adicional para que funcione con MySQL, tengo instalado el connector de MySQL...
No funciona directamente pero los Enterprise Libraries tienen el código fuente y puedes modificarlo para que tenga soporte para mysql (así lo hice en un proyecto)

Debes bajar el MySQLConnector para agregarlo a la referencia de los EL e implementar sus clases

te paso una referencia para que tengas una mejor idea

http://www.codeproject.com/KB/databa...h_Ent_Lib.aspx
http://www.codeproject.com/KB/databa..._for_MSEL.aspx

Salu2
  #7 (permalink)  
Antiguo 28/05/2008, 14:15
 
Fecha de Ingreso: octubre-2003
Ubicación: La Paz - Bolivia
Mensajes: 116
Antigüedad: 20 años, 7 meses
Puntos: 1
Respuesta: Conversion CS a VB

Gracias RootK voy a ver las referencias, y te comento como voy
__________________
"El viento puede soplar fuerte, pero la montaña no lo reverencia"
  #8 (permalink)  
Antiguo 03/06/2008, 10:02
 
Fecha de Ingreso: octubre-2003
Ubicación: La Paz - Bolivia
Mensajes: 116
Antigüedad: 20 años, 7 meses
Puntos: 1
Respuesta: Conversion CS a VB

Hola Rootk he intentado integrar el MySQL con el EL como en tu primer ejemplo pero me sale el siguiente error

Warning as Error: Argument type 'MySql.Data.MySqlClient.MySqlDbType' is not CLS-compliant C:\EntLib4Src\Blocks\Data\Src\Data\MySql\MySqlData base.cs

a que se debe eso espero tu ayuda graciassss
__________________
"El viento puede soplar fuerte, pero la montaña no lo reverencia"
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:04.