简体   繁体   中英

login sql server 2005 on vb6

can anyone help me how to make the connection in the sub login to be usable by the sub consultationlistviewsource? I know I need a variable but overall I board not SUCCESSFUL. i got always an error "Invalid permission specification" on the line of the sub cn.Open ConsultationListviewSource. sorry for my bad english

Option Explicit

Private cn As ADODB.Connection
Private record As ADODB.Recordset
Private list As ListItem

Public Sub ConsultationListviewSource(ByRef objlistview As Object, ByVal          table As String)

cn.Open
record.Open "SELECT *  FROM [" & table & "]", cn, adOpenDynamic,   adLockOptimistic

If record.Fields.Count = 5 Then
    For k = 0 To 4
        With objlistview.ColumnHeaders
            .Add , , record.Fields(k).Name, objlistview.Width / 2
        End With
    Next k

    record.MoveFirst
    While record.EOF = False
        Set list = objlistview.ListItems.Add(, , record.Fields(0))
        list.SubItems(1) = record.Fields(1)
        list.SubItems(2) = record.Fields(2)
        list.SubItems(3) = record.Fields(3)
        list.SubItems(4) = record.Fields(4)
        record.MoveNext
    Wend
End If
record.Close
End Sub

Sub login(textusername As Variant, textpassword As Variant)
On Error GoTo erreur
Set cn = New ADODB.Connection
Set record = New ADODB.Recordset
cn.ConnectionString = "Provider=SQLOLEDB;Data     Source=localhost\SQLExpress;Initial Catalog=bdd_trigger_salarie;User    ID=textusername;Password=textpassword"
cn.Open
MsgBox "bienvenue", vbInformation, "Login"
Exit Sub
erreur:
If Err.Number = -2147217843 Then
    MsgBox "nom d'utilisateur et/ou mot de passe incorrect", vbInformation,   "Login"
End If
End Sub

Private Sub Class_Terminate()
cn.Close
Set cn = Nothing
Set record = Nothing
End Sub

I forgot a lot about VB6 so cannot be 100% sure, but I see one problem in you code: you opened the connection in Sub Login , did not close it, and tried to open it in Sub ConsultationListviewSource again. The quick fix will be removing the cn.Open line in the ConsultationListviewSource sub.

But it is not best practice. You should try to minimize the time the connection is opened and close the connection immediately after use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM