qtal amigos, ps quiero que me ayuden en esto de conexion con base de datos sql2005, este es mi codigo, pero no logro conectar, ya que deberia boterme un mensaje si lo haria. Ayudenme pleaseeeeeee :-(
Public cn As ADODB.Connection
_____________________________________________________________
Public Sub Main()
Dim Y As String
Dim X As String
Dim n As String
Dim AdoErr As ADODB.Error
banderatipocambio = False
banderatipocambio2 = False
Set cn = New ADODB.Connection
On Error GoTo TrataError
Y = "servidorticom"
X = "ticom"
n = "sa"
With cn
.Provider = "SQLOLEDB"
Beep
For Each AdoErr In cn.Errors
MsgBox AdoErr.Description
Next
.ConnectionString = "Data Source=" & Y & ";" & _
"Initial Catalog=" & X & ";" & _
"User Id=" & n & ";" & _
"Password="
.Open
End With
MsgBox "Conexión Conforme a " & Y
TrataError:
mdiPrincipal.Show
'frmNum2Let.Show
End Sub
Tienes instalado el SQL Server 2005?
Tienes Creada la Base de Datos SQL?
Saludos
Te diré una cosa a mi no me gusta para nada estar invocando a cada rato en cada botón y en cada acción la conexión tal cual la realizas en el sub main()
Actualmente ya no trato con vb6 (me pase a .NET por que me lo exigieron y como no es mucha la diferencia...).
Este es un Modulo de Clase tal cual manejo en mis conexiones (Sirve para modo Multi-Hilos que es como lo trabajo)
'
' /////////////////////////////////////////////////////////////
' // Autor: BlackZeroX ( Ortega Avila Miguel Angel ) //
' // //
' // Web: http://InfrAngeluX.Sytes.Net/ //
' // //
' // |-> Pueden Distribuir Este Codigo siempre y cuando //
' // no se eliminen los creditos originales de este codigo //
' // No importando que sea modificado/editado o engrandesido //
' // o achicado, si es en base a este codigo es requerido //
' // el agradacimiento al autor. //
' /////////////////////////////////////////////////////////////
Option Explicit On
Imports MySql.Data.MySqlClient
Public Class Cls_Ado
' // Constantes de conexión a MySQL por ADO .NET!¡.
Private Const PreStrConexion As String = "Server=(ip);Database=(db);user id=(u);password=(p);Connect Timeout=30;"
Private Const PREDBIpServer As String = "127.0.0.1" ' // Predeterminado Default
Private Const PREDBUser As String = "root" ' // Predeterminado Default
Private Const PREDBPass As String = "root" ' // Predeterminado Default
Private Const PREDBIni As String = "Integra" ' // Predeterminado
' // Otros.
Private StrConexion As String
Private MySQLConnectionString As String
' // .||.
Private MyDataAdapter As MySqlDataAdapter
Private MyCommandBuilder As MySqlCommandBuilder
' // .||.
Private DBIpServer As String
Private DBUser As String
Private DBPass As String
Private DBIni As String
Private PrivDataSet As DataSet
Private PrivSqlQuery As String
Private PrivTable As String
Private PrivThisTheardTag As String
Event ErrorEvent(ByVal Ex As Exception, ByVal Cancel As Boolean)
Event Finish()
Public Property StringConection() As String
Get
Return StrConexion
End Get
Set(ByVal value As String)
StrConexion = value
End Set
End Property
Public Property This_Theard_Tag() As String
Get
Return PrivThisTheardTag
End Get
Set(ByVal value As String)
PrivThisTheardTag = value
End Set
End Property
Public Property ServerIP() As String
Get
Return DBIpServer
End Get
Set(ByVal value As String)
DBIpServer = value
End Set
End Property
Public Property ServerDataBase() As String
Get
Return DBIni
End Get
Set(ByVal value As String)
DBIni = value
End Set
End Property
Public Property ServerPassword() As String
Get
Return DBPass
End Get
Set(ByVal value As String)
DBPass = value
End Set
End Property
Public Property ServerUserName() As String
Get
Return DBUser
End Get
Set(ByVal value As String)
DBUser = value
End Set
End Property
Public Function OpenDBMySql(ByVal pServidorIPDNS As String, ByVal pDBuser As String, ByVal pDBPass As String, _
ByVal pBDD As String, ByVal SqlQuery As String, ByVal MySQLConnectionString As String) _
As MySql.Data.MySqlClient.MySqlConnection
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
If StrConexion = Nothing And MySQLConnectionString = Nothing Then
MySQLConnectionString = PreStrConexion
Else
If MySQLConnectionString = Nothing Then
MySQLConnectionString = StrConexion
End If
End If
If pServidorIPDNS = Nothing And DBIpServer = Nothing Then
pServidorIPDNS = PREDBIpServer
Else
If pServidorIPDNS = Nothing Then
pServidorIPDNS = DBIpServer
End If
End If
If pDBuser = Nothing And DBUser = Nothing Then
pDBuser = PREDBUser
Else
If pDBuser = Nothing Then
pDBuser = DBUser
End If
End If
If pDBPass = Nothing And DBPass = Nothing Then
pDBPass = PREDBPass
Else
If pDBPass = Nothing Then
pDBPass = DBPass
End If
End If
If pBDD = Nothing And DBIni = Nothing Then
pBDD = PREDBIni
Else
If pBDD = Nothing Then
pBDD = DBIni
End If
End If
MySQLConnectionString = MySQLConnectionString.Replace("(ip)", pServidorIPDNS). _
Replace("(db)", pBDD).Replace("(u)", pDBuser).Replace("(p)", pDBPass)
If Not MyDataAdapter Is Nothing Then
MyDataAdapter = Nothing
End If
If Not MyCommandBuilder Is Nothing Then
MyCommandBuilder = Nothing
End If
Try
MyADOConnection = New MySql.Data.MySqlClient.MySqlConnection(MySQLConnectionString)
MyDataAdapter = New MySqlDataAdapter(SqlQuery, MyADOConnection)
MyCommandBuilder = New MySqlCommandBuilder(MyDataAdapter)
Return MyADOConnection
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
Return Nothing
End Try
End Function
Public Function ExecuteQuery(ByVal SqlQuery As String, ByVal Tabla As String) As Boolean
Dim MyComandoMysql As New MySqlCommand
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
If Not SqlQuery = "" Then
PrivSqlQuery = SqlQuery
End If
If Tabla = "" Then
PrivTable = Tabla
End If
MyADOConnection = OpenDBMySql(DBIpServer, DBUser, DBPass, DBIni, PrivSqlQuery, StrConexion)
If Not MyADOConnection Is Nothing Then
Try
MyADOConnection.Open()
MyComandoMysql.Connection = MyADOConnection
MyComandoMysql.CommandType = CommandType.Text
MyComandoMysql.CommandText = SqlQuery
MyComandoMysql.ExecuteNonQuery()
MyADOConnection.Close()
RaiseEvent Finish()
Return True
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
Return False
End Try
Else
RaiseEvent Finish()
Return False
End If
End Function
Public Function GetDataSet(Optional ByVal SqlQuery As String = "", Optional ByVal Tabla As String = "") As DataSet
Dim tmpDataSet As New DataSet
Dim MyComandoMysql As New MySqlCommand
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
If Not SqlQuery = "" Then
PrivSqlQuery = SqlQuery
End If
If Not Tabla = "" Then
PrivTable = Tabla
End If
MyADOConnection = OpenDBMySql(DBIpServer, DBUser, DBPass, DBIni, PrivSqlQuery, StrConexion)
If Not MyADOConnection Is Nothing Then
Try
MyADOConnection.Open()
MyComandoMysql.Connection = MyADOConnection
MyComandoMysql.CommandType = CommandType.Text
MyComandoMysql.CommandText = PrivSqlQuery
MyComandoMysql.ExecuteNonQuery()
MyDataAdapter.Fill(tmpDataSet, PrivTable)
MyADOConnection.Close()
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
If Cancel Then
RaiseEvent Finish()
Return Nothing
End If
tmpDataSet = Nothing
End Try
PrivDataSet = tmpDataSet
RaiseEvent Finish()
Return tmpDataSet
Else
RaiseEvent Finish()
Return Nothing
End If
End Function
Public Property This_DataBase() As DataSet
Get
Return PrivDataSet
End Get
Set(ByVal value As DataSet)
PrivDataSet = value
End Set
End Property
Public Property This_SqlQuery() As String
Get
Return PrivSqlQuery
End Get
Set(ByVal value As String)
PrivSqlQuery = value
End Set
End Property
Public Property This_Table() As String
Get
Return PrivTable
End Get
Set(ByVal value As String)
PrivTable = value
End Set
End Property
Public Function UpdateFromDataSet(Optional ByVal pDataSet As DataSet = Nothing, _
Optional ByVal SQLQuery As String = "", _
Optional ByVal Tabla As String = "") _
As Boolean
If Not pDataSet Is Nothing Then
PrivDataSet = pDataSet
End If
If Not SQLQuery = "" Then
PrivSqlQuery = SQLQuery
End If
If Not Tabla = "" Then
PrivTable = Tabla
End If
Dim MyADOConnection As MySql.Data.MySqlClient.MySqlConnection
MyADOConnection = OpenDBMySql(DBIpServer, DBUser, DBPass, DBIni, PrivSqlQuery, StrConexion)
If Not MyADOConnection Is Nothing Then
Try
MyADOConnection.Open()
MyDataAdapter.Update(PrivDataSet, PrivTable)
MyADOConnection.Close()
RaiseEvent Finish()
Return True
Catch ex As Exception
Dim Cancel As Boolean
RaiseEvent ErrorEvent(ex, Cancel)
RaiseEvent Finish()
Return False
End Try
Else
RaiseEvent Finish()
Return False
End If
End Function
End Class
PD.: Se que esta no es la sección de .Net ya. pero e visto muchos códigos de esta manera de hecho donde laboro los programadores hace los mimo (Con alter-ego y código desorganizado (Todo en los formularios y botones... pero bueno)) y bueno viendo que .NET no es mucha la diferencia te puse algo mas o menos igual... solo son equivalencias todo es lo mismo por ejemplo DataSet seri en vb6 el DaraRecord/RecordSet o algo asi era xP...!¡.
Sangriento Infierno Lunar!¡.
si e500 tengo todo instalado lo q no entiendo es xq no se conecta :-(<<-----------ayudamee----------------->>
Lo que veo es que no le estas pasando la password..
Intenta con este otro codigo
'para abrir
Public Sub Open_cn()
On Error GoTo errhandler
Set cmd = New ADODB.Command
Set cn = New ADODB.Connection
With cn
.Provider = "MSDASQL;DRIVER={SQL Server};SERVER=" & strServer & ";trusted_connection=no;user id= " & strUID & ";password=" & strPWD & " ;database=" & strDatabase & ";"
.Open
End With
cnOpen = True
Exit Sub
errhandler:
Call MsgBox("Error.", vbOKOnly, "Error")
strServer = ""
cnOpen = False
End Sub
'Para cerrar
Public Sub Close_cn()
If cnOpen = True Then
cn.Close
Set cn = Nothing
cnOpen = False
End If
End Sub
Cita de: leliCabello en 3 Mayo 2010, 15:48 PM
si e500 tengo todo instalado lo q no entiendo es xq no se conecta :-(<<-----------ayudamee----------------->>
Podria ser que Sql Server 2005 no esta en:
Mixed Mode (Windows Authentication and SQL Server Authentication)
en ese caso trata de conectar con una "Autenticación de Windows", prueba con este enlace para saber si hiciste todo bien con las referencias.
http://www.elguille.info/vb/bases/ADO/SQLServer2005_Visual_Basic_6.htm
Además instala el
Microsoft SQL Server Management Studio Express y prueba desde ahí, para saber si tu usuario y contraseña pueden conectarse sin problemas.