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

    [VB.NET] Problema lettura CAMPO da QUERY

    Ragazzi scusate se rompo troppo ma voglio finire questo mio applicativo
    Devo leggere un campo da una query , però senza usare il while , mi spiego con il codice
    codice:
        <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Elimina gli oggetti prima che siano esterni all'ambito")>
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            'Prendo i dati da smsservergateway e li confronto con smspanel
            Dim Comando_SmsGateway As New OleDbCommand
            Dim Comando_SmsPanel As New OleDbCommand
            Dim Adapter_SmsGateway As New OleDbDataAdapter
            Dim Adapter_SmsPanel As New OleDbDataAdapter
            Dim Reader_SmsGateway As OleDbDataReader
            Dim Reader_SmsPanel As OleDbDataReader
            Dim ANUMBER As String
            Dim SMSMESSAGE As String
            Dim RECEIVEDDATE As Date
            Dim NUMERO As String
            Dim EMAIL As String
            Dim TESTO As String
    
            Comando_SmsGateway.Connection = Connessione_server
            Comando_SmsGateway.CommandText = "SELECT * FROM INBOX"
            Comando_SmsGateway.CommandType = CommandType.Text
            Adapter_SmsGateway.SelectCommand = Comando_SmsGateway
            Connessione_server.Open()
            Reader_SmsGateway = Comando_SmsGateway.ExecuteReader()
    
            While Reader_SmsGateway.Read
                ANUMBER = Reader_SmsGateway("DEVICECODE")
                SMSMESSAGE = Reader_SmsGateway("SMSMESSAGE")
                RECEIVEDDATE = Reader_SmsGateway("RECEIVEDDATE")
                TESTO = "Ricevuto da : " & ANUMBER & Chr(13) & "Testo : " & SMSMESSAGE & Chr(13) & "Ricevuto il : " & RECEIVEDDATE
                Comando_SmsPanel.Connection = Connessione
                Comando_SmsPanel.CommandText = "SELECT * FROM numeri INNER JOIN email ON email.id_email = numeri.idemail WHERE numero = @ANUMBER"
                Comando_SmsPanel.CommandType = CommandType.Text
                Comando_SmsPanel.Parameters.AddWithValue("@ANUMBER", ANUMBER)
                Adapter_SmsPanel.SelectCommand = Comando_SmsPanel
                Connessione.Open()
                Reader_SmsPanel = Comando_SmsPanel.ExecuteReader()
                'NUMERO = Reader_SmsPanel("numero")
                'EMAIL = Reader_SmsPanel("email")
                Connessione.Close()
                InviaEmail(EMAIL, TESTO)
                System.Threading.Thread.Sleep(500)
                If BackgroundWorker1.CancellationPending Then
                    e.Cancel = True
                    Exit While
                End If
            End While
            Connessione_server.Close()
        End Sub
    nella zona della query
    codice:
       Comando_SmsPanel.Connection = Connessione
                Comando_SmsPanel.CommandText = "SELECT * FROM numeri INNER JOIN email ON email.id_email = numeri.idemail WHERE numero = @ANUMBER"
                Comando_SmsPanel.CommandType = CommandType.Text
                Comando_SmsPanel.Parameters.AddWithValue("@ANUMBER", ANUMBER)
                Adapter_SmsPanel.SelectCommand = Comando_SmsPanel
                Connessione.Open()
                Reader_SmsPanel = Comando_SmsPanel.ExecuteReader()
                'NUMERO = Reader_SmsPanel("numero")
                'EMAIL = Reader_SmsPanel("email")
                Connessione.Close()
                InviaEmail(EMAIL, TESTO)
    dovrei riuscire a prendere l'email e il numero dalla query

    codice:
    omando_SmsPanel.CommandText = "SELECT * FROM numeri INNER JOIN email ON email.id_email = numeri.idemail WHERE numero = @ANUMBER"
                Comando_SmsPanel.CommandType = CommandType.Text
                Comando_SmsPanel.Parameters.AddWithValue("@ANUMBER", ANUMBER)
    però per farlo con execute reader avrei bisogno di un ciclo while ma io non voglio usarlo .. xke è gia all interno di altro ciclo ... in php si puo fare senza problemi .. si può anche in vb.net?
    Programmatore - Consulenza informatica -
    Web Design - Professionista I.T.
    ---------------------------------------
    http://www.malorgio.it

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Ovvio che puoi evitare di usare il DataReader se devi leggere solo un campo, mi correggo una riga di un campo.
    Mi spiego con un esempio, DB con tre campi Nome, Cognome, Nick (chiave primaria).

    codice:
     Dim objdatadapter As New OleDbDataAdapter
            Dim objdataset As New DataSet
            Dim obj As DataTable
            Dim connessione As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\db1.mdb")
            Dim comando As OleDbCommand = New OleDbCommand()
            comando.Connection = connessione
            comando.CommandText = "SELECT Nome FROM Anagrafica WHERE Nick='adoNet'"
            comando.CommandType = CommandType.Text
            objdatadapter.SelectCommand = comando
            connessione.Open()
            Dim nome As String = comando.ExecuteScalar.ToString
    Sbagliare è umano, perseverare è diabolico.

  3. #3
    e io ne devo leggere solo due

    codice:
    Comando_SmsPanel.CommandText = "SELECT numero,email FROM numeri INNER JOIN email ON email.id_email = numeri.idemail WHERE numero = @ANUMBER"
                Comando_SmsPanel.CommandType = CommandType.Text
                Comando_SmsPanel.Parameters.AddWithValue("@ANUMBER", ANUMBER)
    Programmatore - Consulenza informatica -
    Web Design - Professionista I.T.
    ---------------------------------------
    http://www.malorgio.it

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Mi spieghi qual'è la difficoltà nell'utilizzare il dataReader ?
    Sbagliare è umano, perseverare è diabolico.

  5. #5

  6. #6
    non è identico , perchè se io eseguo questa sub routine (che tra l'altro è di background)
    codice:
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            'Prendo i dati da smsservergateway e li confronto con smspanel
            Dim Comando_SmsGateway As New OleDbCommand
            Dim Comando_SmsPanel As New OleDbCommand
            Dim Adapter_SmsGateway As New OleDbDataAdapter
            Dim Adapter_SmsPanel As New OleDbDataAdapter
            Dim Reader_SmsGateway As OleDbDataReader
            Dim Reader_SmsPanel As OleDbDataReader
            Dim ANUMBER As String
            Dim SMSMESSAGE As String
            Dim RECEIVEDDATE As Date
            Dim DEVICECODE As String
            'Dim NUMERO As String
            Dim EMAIL As String
            Dim TESTO As String
    
            Comando_SmsGateway.Connection = Connessione_server
            Comando_SmsGateway.CommandText = "SELECT * FROM INBOX"
            Comando_SmsGateway.CommandType = CommandType.Text
            Adapter_SmsGateway.SelectCommand = Comando_SmsGateway
            Connessione_server.Open()
            Reader_SmsGateway = Comando_SmsGateway.ExecuteReader()
    
            While Reader_SmsGateway.Read
                ANUMBER = Reader_SmsGateway("ANUMBER")
                DEVICECODE = Reader_SmsGateway("DEVICECODE")
                SMSMESSAGE = Reader_SmsGateway("SMSMESSAGE")
                RECEIVEDDATE = Reader_SmsGateway("RECEIVEDDATE")
                TESTO = "Ricevuto da : " & ANUMBER & Chr(13) & "Testo : " & SMSMESSAGE & Chr(13) & "Ricevuto il : " & RECEIVEDDATE
                Comando_SmsPanel.Connection = Connessione
                Comando_SmsPanel.CommandText = "SELECT email AS email FROM numeri INNER JOIN email ON email.id_email = numeri.idemail WHERE numero = @DEVICECODE"
                Comando_SmsPanel.CommandType = CommandType.Text
                Comando_SmsPanel.Parameters.AddWithValue("@DEVICECODE", DEVICECODE)
                Adapter_SmsPanel.SelectCommand = Comando_SmsPanel
                Connessione.Open()
                Reader_SmsPanel = Comando_SmsPanel.ExecuteReader()
                Try
                    While Reader_SmsPanel.Read()
                        EMAIL = Reader_SmsPanel.Item("email")
                    End While
    
                Catch ex As Exception
    
                End Try
                MessageBox.Show(DEVICECODE & " " & ANUMBER & " " & SMSMESSAGE & " " & RECEIVEDDATE & " " & EMAIL & " " & TESTO)
                Connessione.Close()
                'InviaEmail(EMAIL, TESTO)
                System.Threading.Thread.Sleep(1500)
                If BackgroundWorker1.CancellationPending Then
                    e.Cancel = True
                    Exit While
                End If
            End While
            Connessione_server.Close()
        End Sub
    Mi esce fuori un eccezione alla riga
    codice:
    Reader_SmsPanel = Comando_SmsPanel.ExecuteReader()
    che dice :

    Impossibile utilizzare oggetti COM separati dai relativi RCW sottostanti.
    Programmatore - Consulenza informatica -
    Web Design - Professionista I.T.
    ---------------------------------------
    http://www.malorgio.it

  7. #7
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    970
    Non capisco al momento perchè tu abbia la necessità di utilizzare il secondo DataReder all'interno del ciclo del primo DataReder.

    Piuttosto farei così:

    codice:
        Comando_SmsGateway.Connection = Connessione_server
            Comando_SmsGateway.CommandText = "SELECT * FROM INBOX"
            Comando_SmsGateway.CommandType = CommandType.Text
            Adapter_SmsGateway.SelectCommand = Comando_SmsGateway
            Connessione_server.Open()
            Reader_SmsGateway = Comando_SmsGateway.ExecuteReader()
    
            Comando_SmsPanel.Connection = connessione
            Comando_SmsPanel.CommandText = "SELECT email AS email FROM numeri INNER JOIN email ON email.id_email = numeri.idemail WHERE numero = @DEVICECODE"
            Comando_SmsPanel.CommandType = CommandType.Text
            Comando_SmsPanel.Parameters.AddWithValue("@DEVICECODE", DEVICECODE)
            Adapter_SmsPanel.SelectCommand = Comando_SmsPanel
            connessione.Open()
            Reader_SmsPanel = Comando_SmsPanel.ExecuteReader()
    
            While Reader_SmsGateway.Read
                While Reader_SmsPanel.Read()
                  'Tuo codice
                End While
            End While
    Sbagliare è umano, perseverare è diabolico.

  8. #8
    Grazie pirelli per l'illuminazione , il problema è che se faccio come dici tu come imposto il parametro DEVICECODE nella seconda query?
    Programmatore - Consulenza informatica -
    Web Design - Professionista I.T.
    ---------------------------------------
    http://www.malorgio.it

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.