Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

  • 1 odgovor
  • 2.050 pregleda

Najnovija ↓

Problem kod konekcije sa SQL bazom 09.04.2008. u 14:04 · pre 226 meseci
izmenjeno 1 put, poslednji put Shadowed 09.04.2008. u 14:40
  1. Shadowed09.04.2008. u 14:40zapis iz teksta poruke
#1
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
Potpis
pedja
Re: Problem kod konekcije sa SQL bazom 12.04.2008. u 03:00 · pre 225 meseci #2
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
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme