Visualizzazione dei risultati da 1 a 10 su 10
  1. #1

    [VB.NET] - Conteggio Record con Oledb

    Ciao a tutti
    dovrei inserire un semplice conteggio dei record contenuti in una tabella di access.
    Ho provato in vari modi (è parecchie ore che ci lavoro sopra)...
    Ho provato ad utilizzare il metodo "ExecuteScalar" in quanto i dati provengono da una query di sola lettura... ma NIENTEEEEEEE !!!!

    Io vorrei ottenere una variabile che nel codice ho chiamato "records" che contenga il numero dei record.

    vi posto il codice:

    codice:
     
            Dim Cn As New OleDb.OleDbConnection(ConnString)
            Dim sql, sql1 As String
            Dim cmd1 As New OleDb.OleDbCommand(sql1, Cn)
            Dim records As Integer
            records = cmd1.ExecuteScalar

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Ma la ConnString cosa contiene? E la query dov'e'?

    E poi "NIENTEEE" cosa vuol dire? Hai degli errori? Dei messaggi ...?

  3. #3
    si... hai ragione...
    allora posto tutto il codice...



    codice:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'CONNESSIONE DATABASE
            Dim PercorsoDB As String = "C:\ANA\DB\DB_ANA.mdb"
            'Stringa di Connessione
            Dim ConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Persist Security Info=False;" & _
            "Data Source=" & PercorsoDB & _
            ";" & _
            "User ID=Admin;" & _
            "Jet OLEDB:Database Password=pippo;"
            'Fine Stringa di Connessione
    
            Dim Cn As New OleDb.OleDbConnection(ConnString)
            Dim sql, sql1 As String
            'RECUPERO I VALORI DA UN DATAGRID PER INSERIRLI SUCCESSIVAMENTE SUL DATABASE
            Dim numerorow As Integer
            'INIZIA IL CICLO
            For numerorow = 0 To DataGridView1.RowCount - 1
                Dim c_cognome, c_nome, c_sesso, c_gruppo As String
                Dim c_data As Date
                Cn.Open()
                c_cognome = DataGridView1.Item("COGNOME", numerorow).Value.ToString
                c_nome = DataGridView1.Item("NOME", numerorow).Value.ToString
                c_data = DataGridView1.Item("DATA_NASCITA", numerorow).Value.ToString
                c_sesso = DataGridView1.Item("SESSO", numerorow).Value.ToString
                c_gruppo = VAL_GRUPPOComboBox.Text
                'EFFETTUO UNA QUERY PER VEDERE SUCCESSIVAMENTE SE NELLA TABELLA DEL DATABASE
                'ESISTE GIà UN RECORD CON COGNOME NOME E DATA UGUALE
                sql1 = "SELECT * FROM Person WHERE COGNOME = " & _
                c_cognome & _
                "; SELECT * FROM Person WHERE COGNOME = " & _
                c_cognome & _
                " and NOME = " & _
                c_nome & _
                " and DATA = " & _
                c_data
    
                'RECUPERO IL NUMERO DEI RECORD RESTITUITI DALLA QUERY
                Dim cmd1 As New OleDb.OleDbCommand(sql1, Cn)
                Dim records As Integer
                records = cmd1.ExecuteScalar
    
                If records = 0 Then
                    'INSERISCO I DATI DEL DATAGRID NELLA TABELLA DEL DATABASE
                    sql = "INSERT INTO Person (COGNOME , NOME , DATA , SESSO , GRUPPO ) VALUES ('" + c_cognome + "', '" + c_nome + "', '" + c_data + "', '" + c_sesso + "', '" + c_gruppo + "')"
                    Dim cmd As New OleDb.OleDbCommand(sql, Cn)
                    Dim temp_num As Integer
                    cmd = New OleDb.OleDbCommand(sql, Cn)
    
                    MsgBox("Aggiunto nuovo affiliato: " & c_cognome & " " & c_nome & " " & c_data & " ", , "Informazione su Nuovo Affiliato")
                    temp_num = cmd.ExecuteNonQuery
                    Cn.Close()
    
                Else
                    'RICEVO UN MESSAGGIO DI ERRORE
                    MsgBox("ATTENZIONE, Soggetto già presente!")
                    Cn.Close()
                End If
            Next
            'RIPRENDE IL CICLO
    
    
        End Sub
    L'errore che mi restituisce è sulla riga

    codice:
    records = cmd1.ExecuteScalar
    ed il messaggio di errore è il seguente:

    codice:
    System.Data.OleDb.OleDbException non è stata gestita
      ErrorCode=-2147217900
      Message="Caratteri non previsti dopo la fine dell'istruzione SQL."
      Source="Microsoft JET Database Engine"
      StackTrace:
           in System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
           in System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
           in System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
           in System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
           in System.Data.OleDb.OleDbCommand.ExecuteScalar()
           in anagrafe.FormCoscheAffNucl.Button1_Click(Object sender, EventArgs e) in C:\Users\Francesco\Documents\Visual_Studio_2005\Projects\anagrafe\anagrafe\anagrafe\FormCoscheAffNucl.vb:riga 123
           in System.Windows.Forms.Control.OnClick(EventArgs e)
           in System.Windows.Forms.Button.OnClick(EventArgs e)
           in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           in System.Windows.Forms.Control.WndProc(Message& m)
           in System.Windows.Forms.ButtonBase.WndProc(Message& m)
           in System.Windows.Forms.Button.WndProc(Message& m)
           in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           in System.Windows.Forms.Application.Run(ApplicationContext context)
           in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           in anagrafe.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:riga 81

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Ma scusa, dato l'errore

    "Caratteri non previsti dopo la fine dell'istruzione SQL."

    questa

    sql1 = "SELECT * FROM Person WHERE COGNOME = " & _
    c_cognome & _
    "; SELECT * FROM Person WHERE COGNOME = " & _
    c_cognome & _
    " and NOME = " & _
    c_nome & _
    " and DATA = " & _
    c_data

    ti sembra corretta?

  5. #5
    no... ma sto FONDENDO !!!!
    è da stamattina che mi sono fermato su questa cosa e non riesco a trovare una soluzione.
    Adesso ho modificato la query in

    codice:
    sql1 = "SELECT * FROM Person WHERE COGNOME = c_cognome And NOME = c_nome And Data = c_data"
    e adesso l'errore che restituisce è il seguente:

    codice:
    Nessun valore specificato per alcuni parametri necessari.
    e praticamente si blocca sempre su

    codice:
    records = cmd1.ExecuteScalar

  6. #6
    comunque, ho sistemato la query per bene ed il problema credo che sia solo la riga seguente:

    records = cmd1.ExecuteScalar

    che non so se è giusta... l'ho vista scritta sul web in vari modi...
    così...
    records = CInt(cmd1.ExecuteScalar)
    o così...
    records = cmd1.ExecuteScalar()
    o così...

    Dim records As Integer = CType(cmd.ExecuteScalar(), Integer)

  7. #7
    RISOLTO.....
    Era proprio la query...
    posto quella corretta:
    codice:
    sql1 = "SELECT COUNT(*) FROM Person WHERE COGNOME = '" & c_cognome & "' AND NOME = '" & c_nome & "' AND DATA = '" & c_data & "'"

    c'è solo un ultimo problema...
    la data, sul database, è impostato come campo testo.
    Se la imposto come campo di tipo data,
    la query mi dà errore.
    Come faccio a formattarla in maniera corretta?

  8. #8
    RISOLTO ANCHE IL PROBLEMA DELLA DATA...
    NON ANDAVA SCRITTO COSì:
    codice:
    AND DATA = '" & c_data & "'
    bensì
    così:
    codice:
    AND DATA = #" & c_data & "#
    grazie lo stesso di tutto OREGON...
    alla prox...

  9. #9
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Sì ... ovviamente era la query ... ma fai attenzione perche' la data con Access deve essere specificata nel formato mm/dd/yyyy ... se non lo fai potresti avere sorprese nei risultati ...

  10. #10
    Infatti...
    grazie ancora.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.