VB.Net/Database ADO.net/Scalar Data

Материал из VB Эксперт
Версия от 12:44, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Execute Row Count: Scalar data

Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient

Public Class MainClass
    Shared Dim WithEvents con As SqlConnection
    Shared Sub Main()
        con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI") 
        Dim cmd As New SqlCommand()
        cmd.rumandText = "SELECT COUNT(*) FROM Employee"
        cmd.Connection = con
        Try
            con.Open()
            Console.WriteLine("{0} - {1}", _
                            "cmd.ExecuteScalar()", cmd.ExecuteScalar().ToString())
        Finally
            con.Close()
        End Try
    End Sub
End Class


ExecuteScalar: select count(*) from your table

Imports System
Imports System.Data
Imports System.Data.SqlClient
public class MainClass
   Shared Sub Main()
      Dim thisConnection As New SqlConnection("server=(local)\SQLEXPRESS;" & _
          "integrated security=sspi;database=MyDatabase")
      "Create Command object
      Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()
      Try
         " Open Connection
         thisConnection.Open()
         Console.WriteLine("Connection Opened")
         "Create Command objects
         Dim scalarCommand As New SqlCommand("SELECT COUNT(*) FROM Employee", thisConnection)
         " Execute Scalar Query
         Console.WriteLine("Before INSERT, Number of Employees = {0}", _
            scalarCommand.ExecuteScalar())

      Catch ex As SqlException
         " Display error
         Console.WriteLine("Error: " & ex.ToString())
      Finally
         " Close Connection
         thisConnection.Close()
         Console.WriteLine("Connection Closed")
      End Try
   End Sub
End Class


Mulitple results: Get result set from two "select" command

Imports System
Imports System.Collections
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Windows.Forms
Imports System.Data.SqlClient

Public Class MainClass
    Shared Dim WithEvents con As SqlConnection
    Shared Sub Main()
        con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI") 
        Dim cmd As New SqlCommand()
        cmd.rumandText = "SELECT * FROM Employee; SELECT * FROM Employee"
        cmd.Connection = con
        Try
            con.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            Do
                While reader.Read()
                    Console.WriteLine(reader(0) & vbTab & reader(1))
                End While
            Loop While reader.NextResult()
            reader.Close()
        Finally
            con.Close()
        End Try
    End Sub
End Class