[ pedja_mil @ 09.04.2008. 13:04 ] @
Imam sledeći problem:

Kada pokusam da se povežem sa SQL bazom, iz VB.NET 2005 aplikacije, koristio sam sledeći kod:

Code:
Private Sub OpenDataReader()
        
        Dim strConn As String = "Server=localhost;Database=Northwind;" + _
"Integrated Security=SSPI"
        Dim cnnNwind As SqlConnection = New SqlConnection(strConn)
        cnnNwind.Open()

        
        Dim strSQL As String = "SELECT * FROM Shippers"
        strSQL += "; SELECT EmployeeID,FirstName,LastName FROM Employees"
        Dim cmdReader As SqlCommand = New SqlCommand(strSQL, cnnNwind)
        cmdReader.CommandText = CommandType.Text

        
        Dim sdrReader As SqlDataReader = cmdReader.ExecuteReader(CommandBehavior.CloseConnection)
        sdrReader = cmdReader.ExecuteReader
        With sdrReader
            If .HasRows Then
                While .Read
                    lstShippers.Items.Add(.Item(0).ToString + " - " + .Item(1).ToString)
                End While
                While .NextResult
                    While .Read
                        lstEmployees.Items.Add(.Item(0).ToString + " - " + .Item(1).ToString + " " + .Item(2).ToString)
                    End While
                End While
            End If
            .Close()
        End With
    End Sub


Kada se kod iskompajlira javlja gresku "Incorrect syntax near '1' " u liniji koda gde sam definisao promenljivu sdrReader




[Ovu poruku je menjao Shadowed dana 09.04.2008. u 14:40 GMT+1]
[ Djoks @ 12.04.2008. 02:00 ] @
Pokušaj ovako:

Code:

        Using Cmd As New SqlCommand("select * from shippers; select employeeid, firstname, lastname from employees", _
                                    New SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI"))

            Cmd.Connection.Open()

            Using Reader As SqlDataReader = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
                With Reader
                    If .HasRows Then
                        While .Read
                            lstShippers.Items.Add(String.Format("{0} - {1}", .Item(0).ToString, .Item(1).ToString))
                        End While
                        While .NextResult
                            While .Read
                                lstEmployees.Items.Add(String.Format("{0} - {1} {2}", .Item(0).ToString, .Item(1).ToString, .Item(2).ToString))
                            End While
                        End While
                    End If
                End With
            End Using
        End Using


ali bih ja radije ovako (zbog preglednosti i jednostavnosti):

Code:

        Using ds As New DataSet
            Using da As New SqlDataAdapter("select * from shippers; select employeeid, firstname, lastname from employees", _
                                           "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI")
                da.Fill(ds)

                For Each Row As DataRow In ds.Tables(0).Rows
                    lstShippers.Items.Add(String.Format("{0} - {1}", Row(0).ToString, Row(1).ToString))
                Next

                For Each Row As DataRow In ds.Tables(1).Rows
                    lstEmployees.Items.Add(String.Format("{0} - {1} {2}", Row(0).ToString, Row(1).ToString, Row(2).ToString))
                Next
            End Using
        End Using