borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Login SQL server)Next Topic (Run-time Error assigning text to/from textbox - error 2185) New Topic New Poll Post Reply
AndreaVB Forum : Database : used loop in sql? Solved Topic
Poster Message
luckyboy
Level: VB Lord

Registered: 05-05-2005
Posts: 160

icon used loop in sql?

hi all. can i use loop in sql?
how can i move next record?


____________________________
Please help out.

17-05-2006 at 02:53 AM
View Profile Send Email to User Show All Posts | Quote Reply
ba1959nh
Level: Scholar

Registered: 10-05-2006
Posts: 39
icon Re: used loop in sql?

I think that you can use a For - Next loop if your data is in a DataSet.

Below is a code snippet.

I hope this helps.


'Set up the keys to data access, the DataSet, Connection and Adapter.
Dim myDataSet As System.Data.DataSet
Dim myConnection As System.Data.SqlClient.SqlConnection
Dim myAdapter As System.Data.SqlClient.SqlDataAdapter
'The connection string should point to your local SQL Server
Dim connString As String = "Data Source=Gryphon;" & _
          "Initial Catalogubs;" & _
          "Integrated Security=SSPI"
'Open the connection
myConnection = New SqlConnection(connString)
'The Adapter has the command in it.
myAdapter = New SqlDataAdapter("select * from Titles", myConnection)
'Fill the dataset with data using the adapter.
myDataSet = New DataSet
myAdapter.Fill(myDataSet, "Titles")
'Loop through the rows and write to the Output window.
Dim row As System.Data.DataRow
For Each row In myDataSet.Tables(0).Rows
    Debug.Write(row(1).ToString)
Next

17-05-2006 at 03:47 AM
View Profile Send Email to User Show All Posts | Quote Reply
luckyboy
Level: VB Lord

Registered: 05-05-2005
Posts: 160
icon Re: used loop in sql?

can i used loop in sql function or procedure?
how can i used move next in sql function or procedure?

____________________________
Please help out.

17-05-2006 at 05:59 AM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: used loop in sql?

Hi Lucky

This is how I do loops in sql

declare @RowCount int
declare @id int

set @RowCount = 1
set @id = 0

while @RowCount > 0
begin

    select top 1 * from YourTable where YourTableID > @id order by YourTableID

    set @RowCount = @@RowCount

    if @RowCount > 0
    begin

        set @ID = YourTableID

        -- do your stuff here
  

    end
end



Steve  

[Edited by steve_w on 17-05-2006 at 08:50 AM GMT]

17-05-2006 at 08:49 AM
View Profile Send Email to User Show All Posts | Quote Reply
ba1959nh
Level: Scholar

Registered: 10-05-2006
Posts: 39
icon Re: used loop in sql?

They always make it look so easy.

This forum is really awesome !!!


P.S.
I hope that my post didn't add to your confusion.



[Edited by ba1959nh on 17-05-2006 at 05:47 PM GMT]

17-05-2006 at 05:45 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Database : used loop in sql? Solved Topic
Previous Topic (Login SQL server)Next Topic (Run-time Error assigning text to/from textbox - error 2185) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder