Visual Basic Foro

Programación => Bases de Datos => Mensaje iniciado por: obethermy en Octubre 09, 2015, 11:58:04 am

Título: Opciones de conexion en Mysql con visual basic
Publicado por: obethermy en Octubre 09, 2015, 11:58:04 am
Código: (VB) [Seleccionar]
enum Code:
Public Enum MyOption
        'recommended option values for various configurations.
        optVB = 3                      'Microsoft Access, Visual Basic
        optLargeTables = 2049          'Large tables with too many rows
        optSybasePB = 135168           'sysbase powerbuilder
        optLT_nocache = 3145731         'Large tables with no-cache results
 
        'other Option Flags
        optFieldLength = 1              'FLAG_FIELD_LENGTH       'Don't Optimize Column Width
        optFoundRows = 2                'FLAG_FOUND_ROWS         'Return Matching Rows
        optDebug = 4                    'FLAG_DEBUG              'Trace Driver Calls To myoptbc.log
        optBigPacket = 8                'FLAG_BIG_PACKETS        'Allow Big Results
        optNoPrompt = 16                'FLAG_NO_PROMPT          'Don't Prompt Upon Connect
        optDynamicCursor = 32           'FLAG_DYNAMIC_CURSOR     'Enable Dynamic Cursor
        optNoSchema = 64                'FLAG_NO_SCHEMA          'Ignore # in Table Name
        optNoDefaultCursor = 128        'FLAG_NO_DEFAULT_CURSOR  'User Manager Cursors
        optNoLocale = 256               'FLAG_NO_LOCALE          'Don't Use Set Locale
        optPadSpace = 512               'FLAG_PAD_SPACE          'Pad Char To Full Length
        optFullColumnNames = 1024       'FLAG_FULL_COLUMN_NAMES  'Return Table Names for SQLDescribeCol
        optCompressedProto = 2048       'FLAG_COMPRESSED_PROTO   'Use Compressed Protocol
        optIgnoreSpace = 4096           'FLAG_IGNORE_SPACE       'Ignore Space After Function Names
        optNamedPipe = 8192             'FLAG_NAMED_PIPE         'Force Use of Named Pipes
        optNoBigInt = 16384             'FLAG_NO_BIGINT          'Change BIGINT Columns to Int
        optNoCatalog = 32768            'FLAG_NO_CATALOG         'No Catalog
        optUseMyCnf = 65536             'FLAG_USE_MYCNF          'Read Options From my.cnf
        optSafe = 131072                'FLAG_SAFE               'Safe
        optNoTransactions = 262144      'FLAG_NO_TRANSACTIONS    'Disable transactions
        optLogQuery = 524288            'FLAG_LOG_QUERY          'Save queries to myodbc.sql
        optNoCache = 1048576            'FLAG_NO_CACHE           'Don't Cache Result (forward only cursors)
        optForwardCursor = 2097152      'FLAG_FORWARD_CURSOR     'Force Use Of Forward Only Cursors
        optAutoReconnect = 4194304      'FLAG_AUTO_RECONNECT     'Enable auto-reconnect.
        optAutoIsNull = 8388608         'FLAG_AUTO_IS_NULL       'Flag Auto Is Null
        optZeroDateToMin = 16777216     'FLAG_ZERO_DATE_TO_MIN   'Flag Zero Date to Min
        optMinDateToZero = 33554432     'FLAG_MIN_DATE_TO_ZERO   'Flag Min Date to Zero
        optMultiStatements = 67108864   'FLAG_MULTI_STATEMENTS   'Allow multiple statements
        optColumnSizeS32 = 134217728    'FLAG_COLUMN_SIZE_S32    'Limit column size to 32-bit value
End Enum

Public Sub IniciarConexion1()
    Dim CadenaConexion As String
    Dim ConexionTipo As String
    Dim Usuario As String
    Dim Clave As String
    Dim Proveedor As String
    Dim puerto As String
    Dim MiOpciones
    On Error GoTo Error
    Proveedor = "MSDASQL"
    'ConexionTipo = "{MySQL ODBC 5.3 ANSI Driver}"
    ConexionTipo = "{MySQL ODBC 5.1 Driver}"
    Usuario = "root"
    Clave = "1234567890"
    puerto = "3306"
     MiOpciones = optFoundRows + optVB + optBigPacket + optDynamicCursor + _
                optCompressedProto + optNoBigInt + optAutoReconnect
    'CadenaConexion = "driver=" & ConexionTipo & ";server=" & Servidor & ";user=" & Usuario & ";pwd=" & Clave & ";database=" & BasedeDatos & ";option=3;"
    CadenaConexion = "provider=" & Proveedor & ";driver=" & ConexionTipo & ";server=" & servidor & ";user=" & Usuario & ";pwd=" & Clave & ";database=" & BasedeDatos & ";option=" & MiOpciones & ";port=" & puerto & ";"
    With con
        .CommandTimeout = 400
        .ConnectionTimeout = 60
        .CursorLocation = adUseClient
        .Open CadenaConexion
    End With
    Exit Sub
Error:
If Not con Is Nothing Then
  If con.State = adStateOpen Then con.Close
End If
Set con = Nothing
For Each ErrorBD In con.Errors
 Select Case ErrorBD.NativeError
  Case 1049
   MsgBox "Error en nombre de servidor.", vbExclamation
  Case 2003
   MsgBox "Error en datos de ip.", vbExclamation
  Case 3709
   MsgBox "Error no se pudo leer el usuario", vbExclamation
 End Select
Next
End Sub
Título: Re:Opciones de conexion en Mysql con visual basic
Publicado por: eduardo86 en Octubre 12, 2015, 05:19:39 pm
hola me podrias corroborar si en las opciones que pones se puede configurar lo desactivar el autocommit
Título: Re:Opciones de conexion en Mysql con visual basic
Publicado por: obethermy en Octubre 18, 2015, 12:48:21 pm
la variable autocommit
select variable like '%commit%';
set autocommit=on;
set autocommit=off;
 
Esta optcion es cuando te conecta al servidor
Ah te refieres a optNoTransactions = 262144      'FLAG_NO_TRANSACTIONS    'Disable transactions
Son referencia documentadas de la misma pagina oficial mysql
Si la toma en cuenta para la conexion mysql y lo que hace es desactivar trasacciones a la base de datos
para hacer trasacciones esta la pagina oficial mysql http://dev.mysql.com/doc/refman/5.6/en/commit.html (http://dev.mysql.com/doc/refman/5.6/en/commit.html)


Título: Re:Opciones de conexion en Mysql con visual basic
Publicado por: obethermy en Octubre 18, 2015, 01:20:14 pm
Para hacer pruebas si te fijas no puse set autocommit=off  ya que al poner begin en trasaccion ya lo pone. Si usa un programa el rollback va en  etiqueta error

mysql>use test
mysql> CREATE TABLE innotest (campo INT NOT NULL PRIMARY KEY) TYPE = InnoDB;
Query OK, 0 rows affected (0.10 sec)

mysql> INSERT INTO innotest VALUES(1);
Query OK, 1 row affected (0.08 sec)

mysql> INSERT INTO innotest VALUES(2);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO innotest VALUES(3);
Query OK, 1 row affected (0.04 sec)

mysql> SELECT * FROM innotest;
+-------+
| campo |
+-------+
|     1 |
|     2 |
|     3 |
+-------+
3 rows in set (0.00 sec)

De acuerdo, nada espectacular. Ahora veamos como usar transacciones.

mysql> BEGIN;
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO innotest VALUES(4);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM innotest;
+-------+
| campo |
+-------+
|     1 |
|     2 |
|     3 |
|     4 |
+-------+
4 rows in set (0.00 sec)

Si en este momento ejecutamos un ROLLBACK, la transacción no será completada, y los cambios realizados sobre la tabla no tendrán efecto.

mysql> ROLLBACK;
Query OK, 0 rows affected (0.06 sec)

mysql> SELECT * FROM innotest;
+-------+
| campo |
+-------+
|     1 |
|     2 |
|     3 |
+-------+
3 rows in set (0.00 sec)
Ahora vamos a ver que sucede si perdemos la conexión al servidor antes de que la transacción sea completada.

mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO innotest VALUES(4);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM innotest;
+-------+
| campo |
+-------+
|     1 |
|     2 |
|     3 |
|     4 |
+-------+
4 rows in set (0.00 sec)

mysql> EXIT;
Bye

Cuando obtengamos de nuevo la conexión, podemos verificar que el registro no se insertó, ya que la transacción no fue completada.

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 449 to server version: 4.0.13

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.


mysql> SELECT * FROM innotest;
+-------+
| campo |
+-------+
|     1 |
|     2 |
|     3 |
+-------+
3 rows in set (0.00 sec)
Ahora vamos a repetir la sentencia INSERT ejecutada anteriormente, pero haremos un COMMIT antes de perder la conexión al servidor al salir del monitor de MySQL.

mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO innotest VALUES(4);
Query OK, 1 row affected (0.00 sec)

mysql> COMMIT;
Query OK, 0 rows affected (0.02 sec)

mysql> EXIT;
Bye
Una vez que hacemos un COMMIT, la transacción es completada, y todas las sentencias SQL que han sido ejecutadas previamente afectan de manera permanente a las tablas de la base de datos.

Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 450 to server version: 4.0.13

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql> SELECT * FROM innotest;
+-------+
| campo |
+-------+
|     1 |
|     2 |
|     3 |
|     4 |
+-------+
4 rows in set (0.00 sec)