• Viernes 10 de Mayo de 2024, 06:42

Autor Tema:  Conexio Con Odbc  (Leído 2740 veces)

az8

  • Miembro MUY activo
  • ***
  • Mensajes: 111
    • Ver Perfil
Conexio Con Odbc
« en: Martes 7 de Junio de 2005, 20:53 »
0
COMO GENRO UNA CADENA DE CONEXION USANDO UN ODBC PARA CONECTARME A UNA BASE DE DATOS DE SQL SERVER 7

ME HAND DICHO QUE ES MEJOR ESTE TIPO DE CONEXION CUANDO EL SISTEMA CORRERA EN RED......

ESPERO ME PUEDAN AYUDAR

GRACIAS
ATE AZ8

Soultaker

  • Miembro HIPER activo
  • ****
  • Mensajes: 972
  • Nacionalidad: pe
    • Ver Perfil
Re: Conexio Con Odbc
« Respuesta #1 en: Martes 7 de Junio de 2005, 21:06 »
0
Chequeate esta página "Cadenas de Conexión"
Código: Text
  1. Standard Security:
  2. Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"
  3.  
  4. Trusted connection:
  5. Driver={SQL Server};Server=Aron1;Database=pubs;Trusted_Connection=yes;"
  6.  
  7.  
  8. Prompt for username and password:
  9. oConn.Properties("Prompt") = adPromptAlways
  10. oConn.Open "Driver={SQL Server};Server=Aron1;DataBase=pubs;"
  11.  

 :comp:
http]
foros,programacion, c, c++, java,
linux, videojuegos, directX, .NET,
visual basic,robotica,microprocesadores

Para los nuevos usuarios de este Foro, por favor leerse esta discusión.
Cómo Hacer Preguntas De Forma Inteligente

Ojos que no ven... pisan caquita
Camarón que se duerme... muere electrocutado


<<< Harol Moreyra Bendezú >>>

solcito

  • Miembro MUY activo
  • ***
  • Mensajes: 143
    • Ver Perfil
Re: Conexio Con Odbc
« Respuesta #2 en: Martes 7 de Junio de 2005, 21:32 »
0
aZ8, si lo que quieres es una conexion desde una aplicacion a una base de datos lo mejor, creo yo, es hacerlo mediante un archivo, y que tu cadena de conexion no este en codigo del programa.

Para poder hacer esto, crea un nuevo archivo con el bloc de notas, sin nada, vacio, guardalo y cambiale la extension a .udl, vas a ver que el icono va a cambiar, abre de nuevo ese archivo renombrado, que es un nuevo archivo de Vinculo a datos de Microsoft.

Ahi veras 4 solapas:
Solapa PROVEEDOR: ahi seleccionas: "Microsoft OLE DB Provider for SQL Server", luego click en siguente.
Solapa CONEXION:  aca tenemos tres puntos
    1) "Seleccione o escriba el nombre del servidor".  Si tienes instalado el SQL Server en tu maquina, y ahi es a donde te quieres conectar, seleccionala, si no el servidor en donde esta tu base
   2) "Escriba la informacion para iniciar sesión en el Servidor". otra vez si te vas a conectar localmente, selecciona la primer opcion (seguridad integrada de Windows NT)
   3) "Selecciona la base de datos del servidor". ahi va a aparecer una lista de bases de datos que esten en el servidor que hayas seleccionado en el primer punto, selecciona a la que te quieres conectar, y luego Click en PROBAR CONEXION, si has hecho todo correctamente, esto deberia decir que se pudo realizar la conexio.


Bueno ahora tienes que indicarle a tu programa de donde tiene que leer la ruta de conexion:

Código: Text
  1.  
  2. 'Conexion a la base de datos
  3. Public Function CadenaConecc() As String
  4.  
  5.     CadenaConecc = &#34;File Name=&#34; & App.Path & &#34;&#092;cadenaConexion.udl&#34;
  6.  
  7. End Function
  8.  
  9.  
 
por supuesto que el archivo tiene que estar en el mismo directorio que tu aplicacion.

Espero haberte sido de ayuda y que se haya entendido, cualquier cosa vuelve a preguntar.

Saludos
Everything that has a beginning has an end.....

toten

  • Miembro MUY activo
  • ***
  • Mensajes: 140
    • Ver Perfil
Re: Conexio Con Odbc
« Respuesta #3 en: Martes 7 de Junio de 2005, 22:36 »
0
mañana te mando un ejem´plo super bueno de manejar odbc

y miebntras pueds ver uno de mis recursos que es un articulo que trata sobre odbc... mañana te mando la solucion para que la veas y la entiendas mejor

saludos :devil:
[size=109]Andres Eloy Blanco Madriz [ToT3n][/b][/size]
[size=109]Voy Hablando Y Con El Maso Dando...[/size]
[size=109]Solo Soy La Sombra Del Lado Oscuro De Tu Alma...[/size]
[size=109]Y A La Hora De La Verdad Yo Solo Se Que No Se Nada...[/size]
[size=109]|**VENEZUELA**|[/size]

Kev

  • Miembro HIPER activo
  • ****
  • Mensajes: 654
    • Ver Perfil
Re: Conexio Con Odbc
« Respuesta #4 en: Martes 7 de Junio de 2005, 22:56 »
0
Guardar tu cadena de conexion en un archivo aparte seria buena opcion para posterior mantenimiento, es decir, si cambiaras los datos de la base de datos, pero ojo que no debes olvidar encriptar los datos que sino cualquiera se entra y ya fue..... adios info, ya conoces el dicho confianza ni en la camisa  B)

 :comp:  :hola:
La teoria es asesinada tarde o temprano por la experiencia
Albert Einstein
Kev - 3 DCE
-----------------------------------------------------
Articulos:
Convertir texto a imagen en ASP.NET
Trabajando con el Global.asax

Shiquilla

  • Miembro MUY activo
  • ***
  • Mensajes: 188
  • Nacionalidad: es
    • Ver Perfil
    • http://www.songesoft.com
Re: Conexio Con Odbc
« Respuesta #5 en: Miércoles 8 de Junio de 2005, 16:32 »
0
Para crear DSN:

--Módulo para configurar ODBC (Guardalo en un módulo todo este código)
Código: Text
  1. Option Explicit
  2. ' ----------------------------------------------------------
  3. ' ODBC API Declarations
  4. ' ----------------------------------------------------------
  5. ' basic ODBC Declares
  6.  
  7. Declare Function SQLAllocEnv Lib &#34;odbc32.dll&#34; (env As Long) As Integer
  8.  
  9. Declare Function SQLFreeEnv Lib &#34;odbc32.dll&#34; (ByVal env As Long) As Integer
  10.  
  11. Declare Function SQLAllocConnect Lib &#34;odbc32.dll&#34; (ByVal env As Long, _
  12. hDbc As Long) As Integer
  13.  
  14. Declare Function SQLConnect Lib &#34;odbc32.dll&#34; (ByVal hDbc As Long, _
  15. ByVal Server As String, ByVal serverlen As Integer, _
  16. ByVal uid As String, ByVal uidlen As Integer, ByVal pwd As String, _
  17. ByVal pwdlen As Integer) As Integer
  18.  
  19. Declare Function SQLFreeConnect Lib &#34;odbc32.dll&#34; (ByVal hDbc As Long) _
  20. As Integer
  21.  
  22. Declare Function SQLDisconnect Lib &#34;odbc32.dll&#34; (ByVal hDbc As Long) _
  23. As Integer
  24.  
  25. Declare Function SQLAllocStmt Lib &#34;odbc32.dll&#34; (ByVal hDbc As Long, _
  26. hStmt As Long) As Integer
  27.  
  28. Declare Function SQLFreeStmt Lib &#34;odbc32.dll&#34; (ByVal hStmt As Long, _
  29. ByVal EndOption As Integer) As Integer
  30.  
  31. Declare Function SQLExecDirect Lib &#34;odbc32.dll&#34; (ByVal hStmt As Long, _
  32. ByVal sqlString As String, ByVal sqlstrlen As Long) As Integer
  33.  
  34. Declare Function SQLNumResultCols Lib &#34;odbc32.dll&#34; (ByVal hStmt As Long, _
  35. NumCols As Integer) As Integer
  36.  
  37. Declare Function SQLFetch Lib &#34;odbc32.dll&#34; (ByVal hStmt As Long) As Integer
  38.  
  39. Declare Function SQLGetData Lib &#34;odbc32.dll&#34; (ByVal hStmt As Long, _
  40. ByVal Col As Integer, ByVal wConvType As Integer, ByVal lpbBuf As String, _
  41. ByVal dwbuflen As Long, lpcbout As Long) As Integer
  42.  
  43. Declare Function sqlError Lib &#34;odbc32.dll&#34; Alias &#34;SQLError&#34; (ByVal env As _
  44. Long, ByVal hDbc As Long, ByVal hStmt As Long, ByVal SQLState As _
  45. String, NativeError As Long, ByVal Buffer As String, ByVal Buflen As _
  46. Integer, OutLen As Integer) As Integer
  47.  
  48. Declare Function SQLSetConnectOption Lib &#34;odbc32.dll&#34; (ByVal hDbc&, _
  49. ByVal fOption%, ByVal vParam&) As Integer
  50.  
  51. Declare Function SQLSetStmtOption Lib &#34;odbc32.dll&#34; (ByVal hStmt&, _
  52. ByVal fOption%, ByVal vParam&) As Integer
  53.  
  54.  
  55.  
  56. ' misc Constants
  57. Public Const sqlChar = 1
  58. Public Const sqlMaxMsgLen = 512
  59. Public Const sqlFetchNext = 1
  60. Public Const sqlFetchFirst = 2
  61. Public Const sqlStillExecuting = 2
  62. Public Const sqlODBCCursors = 110
  63. Public Const sqlConcurrency = 7
  64. Public Const sqlCursorType = 6
  65.  
  66.  
  67. ' -- ODBC Commands
  68. Public Const ODBC_ADD_DSN = 1&
  69. Public Const ODBC_CONFIG_DSN = 2&
  70. Public Const ODBC_REMOVE_DSN = 3&
  71. Public Const ODBC_ADD_SYS_DSN = 4&
  72. Public Const ODBC_CONFIG_SYS_DSN = 5&
  73. Public Const ODBC_REMOVE_SYS_DSN = 6&
  74. Public Const ODBC_REMOVE_DEFAULT_DSN = 7&
  75.  
  76.  
  77. ' -- ODBC Error Codes
  78. Public Const ODBC_ERROR_GENERAL_ERR = 1
  79. Public Const ODBC_ERROR_INVALID_BUFF_LEN = 2
  80. Public Const ODBC_ERROR_INVALID_HWND = 3
  81. Public Const ODBC_ERROR_INVALID_STR = 4
  82. Public Const ODBC_ERROR_INVALID_REQUEST_TYPE = 5
  83. Public Const ODBC_ERROR_COMPONENT_NOT_FOUND = 6
  84. Public Const ODBC_ERROR_INVALID_NAME = 7
  85. Public Const ODBC_ERROR_INVALID_KEYWORD_VALUE = 8
  86. Public Const ODBC_ERROR_INVALID_DSN = 9
  87. Public Const ODBC_ERROR_INVALID_INF = 10
  88. Public Const ODBC_ERROR_REQUEST_FAILED = 11
  89. Public Const ODBC_ERROR_INVALID_PATH = 12
  90. Public Const ODBC_ERROR_LOAD_LIB_FAILED = 13
  91. Public Const ODBC_ERROR_INVALID_PARAM_SEQUENCE = 14
  92. Public Const ODBC_ERROR_INVALID_LOG_FILE = 15
  93. Public Const ODBC_ERROR_USER_CANCELED = 16
  94. Public Const ODBC_ERROR_USAGE_UPDATE_FAILED = 17
  95. Public Const ODBC_ERROR_CREATE_DSN_FAILED = 18
  96. Public Const ODBC_ERROR_WRITING_SYSINFO_FAILED = 19
  97. Public Const ODBC_ERROR_REMOVE_DSN_FAILED = 20
  98. Public Const ODBC_ERROR_OUT_OF_MEM = 21
  99. Public Const ODBC_ERROR_OUTPUT_STRING_TRUNCATED = 22
  100.  
  101. 'API Command to create a Data Source Name, not used in this example
  102. Public Declare Function SQLCreateDataSource Lib &#34;odbccp32.dll&#34; (ByVal hwnd&, ByVal lpszDS&#036;) As Boolean
  103. 'API to modify/Edit/Create a Data Source Name
  104. Public Declare Function SQLConfigDataSource Lib &#34;odbccp32.dll&#34; (ByVal hwnd As Long, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Boolean
  105.  

Esto sería para crear el ODBC, pon esto en tu formulario:

Código: Text
  1. Public Sub Form_Load()
  2.      ObtenerListadoServidores
  3. End Sub
  4.  
  5. Private Sub ObtenerListadoServidores()
  6.  
  7. Dim ListaServers As NameList
  8. Dim i As Integer
  9.     
  10.     Set oSQLServerApp = New SQLDMO.Application
  11.     Set ListaServers = oSQLServerApp.ListAvailableSQLServers
  12.     '
  13.     For i = 1 To ListaServers.Count
  14.         cbSQLServers.AddItem ListaServers.Item(i)
  15.     Next
  16.     
  17.     If cbSQLServers.ListCount = 1 Then
  18.         cbSQLServers.ListIndex = 0
  19.     Else
  20.         cbSQLServers.ListIndex = -1
  21.     End If
  22.     Set ListaServers = Nothing
  23.     
  24. End Sub
  25.  
  26. Private Sub Command1_Click()
  27.       GenerarODBC
  28. End Sub
  29.  
  30. Private Sub GenerarODBC()
  31. Dim DSN           As String
  32. Dim Server        As String
  33. Dim Address       As String
  34. Dim Database      As String
  35. Dim Description   As String
  36. Dim Security      As String
  37. Dim SqlDriver As String
  38. Dim SQLParameter As String
  39.  
  40.    If cbSQLServers.ListIndex &#60;&#62; -1 Then Server = &#34;SERVER=&#34; & Trim(cbSQLServers.List(cbSQLServers.ListIndex))
  41.    If cbSQLServers.ListIndex &#60;&#62; -1 Then Address = &#34;ADDRESS=&#34; & Trim(cbSQLServers.List(cbSQLServers.ListIndex))
  42.   
  43.   
  44.    DSN = &#34;DSN=NombeODBC&#34;
  45.    Database = &#34;DATABASE=BaseDeDatos&#34;
  46.    Description = &#34;DESCRIPTION=DSN de Prueba&#34; 'Descripcion del ODBC
  47.    Security = &#34;NETWORK=dbmssocn&#34;
  48.    SqlDriver = &#34;SQL Server&#34;
  49.    SQLParameter = DSN & vbNullChar & Server & vbNullChar & Address & vbNullChar & Security & vbNullChar & Database & vbNullChar & Description & vbNullChar & vbNullChar
  50.    SQLConfigDataSource 0&, ODBC_ADD_SYS_DSN, SqlDriver, SQLParameter
  51.  
  52. End Sub
  53.  

Esto sería si lo quieres crear por código el ODBC. Por ejemplo podrías hacer una función de que si no existe el ODBC que lo cree cada vez que entras a la aplicación.

Para abrir una conexion utilizando el ODBC sería hacer esto:
Código: Text
  1. Dim cn As ADODB.Connection
  2. Set cn = New ADODB.Connection
  3. cn.Open &#34;DSN=NombreODBC; UID=Usuario; PWD=Contraseña;&#34;
  4.  

Saludos,
« §hÿqµïllæ »