
Originally Posted by
nlights
I found that conn.resultset.CurrentRowIndex can be used to determine whether or not conn.Execute() found any rows. If not, CurrentRowIndex=0, if yes, CurrentRowIndex=1 right after the query is run. I have tested this on SQL Server 2008, and it works great every time.
I must say, though, that the documentation when it comes to this is misleading at best. The example given in the documentation shows how you can (supposedly) list returned row values using rs.NextRow(), but if you follow this example you always skip the first row! Responses by Alpha Software engineers to rs.NextRow() -related questions here on the forum claim that rs.NextRow() will find the first row in the resultset when it is run for the first time, but this is simply not true. Selwyn FINALLY clarified this in his above post, but the documentation remains wrong. I don't know, maybe SQL Server 2008 resultsets are somehow different when it comes to NextRow() behavior, but I kind of doubt it. Do the people writing A5 documentation bother to check whether or not the examples they give actually work? It took me a while scratching my head with this one until I finally realize that I've been mislead by the A5 documentation.
conn.resultset.CurrentRowIndex IS NOT RELIABLE AND SHOULD NOT BE RELIED ON TO TEST IF A RESULTSET HAS DATA.
the value in this property will depend on the type of sql database you are connected to. for some backends, it might be populated, and for others it might not be.
the only way to check reliably if a result set has data is to call the .nextRow() method.
for example
Code:
dim cn as sql::connection
flag = cn.open("::Name::sqlserver2012_northwind")
flag = cn.Execute("select * from customers where country = 'xx'")
?flag
= .T.
dim rs as sql::ResultSet
rs = cn.ResultSet
?rs.nextrow()
= .F.
.nextRow() does NOT 'consume' the first row of data as you might think. so the following pattern will reliably return all of the data in the resultset
Code:
delete cn
dim cn as sql::connection
flag = cn.open("::Name::sqlserver2012_northwind")
flag = cn.Execute("select * from customers where country = 'france'")
dim rs as sql::ResultSet
rs = cn.ResultSet
flag = rs.nextrow()
dim cities as c
cities = ""
while flag
cities = cities +rs.data("city") + crlf()
flag = rs.nextrow()
end while
for a complete description of how to use sql tables with xbasic i suggest you read the excellent tutorial on the subject.
open the documentation viewer, and search for "Learning Xbasic - Using Xbasic with SQL Tables"
Bookmarks