Senin, 18 Mei 2009

ADONET DATASET count rows SQLSERVER

The DataSet contains copy of the data we requested through the SQL statement. The DataSet consists of DataTableCollection and their DataRelationCollection. The DataTableCollection contains zero or more DataTable objects. The data set may comprise data for one or more members, corresponding to the number of rows. The following VB.NET source code shows how to find the number of rows in a table that resides in the Dataset.

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

sql = "Your SQL Statement Here"

You have to replace the string with your real time variables.

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "Your SQL Statement Here"
connection = New SqlConnection(connetionString)

Try
connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds, "SQL Temp Table")
adapter.Dispose()
command.Dispose()
connection.Close()

MsgBox("Number of row(s) - " & ds.Tables(0).Rows.Count)

Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class

0 komentar: