Groups | Blog | Home
all groups > dotnet datatools > march 2005 >

dotnet datatools : Bug in ExecuteNonQuery doing insert?



Chris
3/16/2005 4:33:02 AM
According to the docs, for the following code, I should get 5 rows returned.
However, when I run this, n = 10. Is that a bug, or something I don't
understand?


Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader
conn = New SqlConnection(Lookups.connStr)
conn.Open()
cmd = conn.CreateCommand
cmd.Connection = conn

Dim x As Integer = 5

cmd.CommandText = "insert into Random (id) " & _
"select top " & x & " id" & _
" from OtherTable order by newid()"
Dim n As Integer

' this should be 5, but returns 10
n = cmd.ExecuteNonQuery()

conn.Close()
conn = Nothing


CT
3/18/2005 8:36:48 AM
Hi Chris,

It works fine here, well I created a sample Random table in the Northwind
database on the local SQL Server 2000 db server, with a single column of
datatype nvarchar and copied from the Customers table (had to change the
SELECT statement to select from the CustomerID column), but the return value
is 5.

I hate to ask, but it is 10 and not -1 you get back, right?

--
Carsten Thomsen
Enterprise Development with VS .NET, UML, AND MSF
http://www.apress.com/book/bookDisplay.html?bID=105

[quoted text, click to view]

Chris
3/18/2005 12:17:07 PM
It's returning 10.

There's a trigger on INSERT on the Random table that updates another table,
and for some reason it's counting those inserts as well. Note that running
the exact same SQL in QueryAnalyzer returns 5 rows, so I'd be interested in
knowing why ExecuteNonQuery does something different, and if there's a way to
turn it off!



[quoted text, click to view]
AddThis Social Bookmark Button