Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Problem kod konekcije sa SQL bazom

[es] :: .NET :: Problem kod konekcije sa SQL bazom

[ Pregleda: 1637 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

pedja_mil
Beograd

Član broj: 131266
Poruke: 23
80.93.231.*



Profil

icon Problem kod konekcije sa SQL bazom09.04.2008. u 13:04 - pre 195 meseci
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]
pedja
 
Odgovor na temu

Djoks
Djordje Najdanovic
Software Developer
Azalea Maritime

Član broj: 1630
Poruke: 268
85.94.122.*

Sajt: www.azalea-maritime.com


Profil

icon Re: Problem kod konekcije sa SQL bazom12.04.2008. u 02:00 - pre 195 meseci
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
 
Odgovor na temu

[es] :: .NET :: Problem kod konekcije sa SQL bazom

[ Pregleda: 1637 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.