VB.Net by API/System.Data.SqlClient/SqlCommand

Материал из VB Эксперт
Перейти к: навигация, поиск

SqlCommand.CommandType

<source lang="vbnet"> Imports System Imports System.Data Imports System.Data.SqlClient

public class MainClass

  Shared Sub Main()
     Dim SqlConnection1 As New SqlConnection("server=(local)\SQLEXPRESS;" & _
         "integrated security=sspi;database=MyDatabase")
     Dim thisCommand As SqlCommand = SqlConnection1.CreateCommand()
     thisCommand.rumandType = CommandType.StoredProcedure
     thisCommand.rumandText = "SelectAllEmployees"
     Try
        SqlConnection1.Open()
        Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
        thisReader.Close()
     Catch ex As System.Data.SqlClient.SqlException
        Dim str As String
        str = "Source : " & ex.Source
        str &= ControlChars.NewLine
        str &= "Exception Message : " & ex.Message
        Console.WriteLine("Database Exception" & str)
     Catch ex As System.Exception
        Dim str As String
        str = "Source : " & ex.Source
        str &= ControlChars.NewLine
        str &= "Exception Message : " & ex.Message
        Console.WriteLine("Non-Database Exception " & str)
     Finally
        If SqlConnection1.State = ConnectionState.Open Then
           Console.WriteLine("Finally block closing the connection")
           SqlConnection1.Close()
        End If
     End Try
  End Sub

End Class


 </source>


SqlCommand.ExecuteReader()

<source lang="vbnet"> Imports System Imports System.Data Imports System.Data.SqlClient

public class MainClass

  Shared Sub Main()
     Dim SqlConnection1 As New SqlConnection("server=(local)\SQLEXPRESS;" & _
         "integrated security=sspi;database=MyDatabase")
     Dim thisCommand As SqlCommand = SqlConnection1.CreateCommand()
     thisCommand.rumandText = "Select * from Employee"
     Try
        SqlConnection1.Open()
        Dim thisReader As SqlDataReader = thisCommand.ExecuteReader()
        Dim str As String = thisReader.GetValue(20).ToString()
        thisReader.Close()
     Catch ex As System.InvalidOperationException
        Dim str As String
        str = "Source : " & ex.Source
        str &= ControlChars.NewLine
        str &= "Exception Message : " & ex.Message
        str &= ControlChars.NewLine
        str &= "Stack Trace : " & ex.StackTrace
        Console.WriteLine("Specific Exception:" & str)
     Catch ex As System.Data.SqlClient.SqlException
        Dim str As String
        str = "Source : " & ex.Source
        str &= ControlChars.NewLine
        str &= "Exception Message : " & ex.Message
        Console.WriteLine("Database Exception" & str)
     Catch ex As System.Exception
        Dim str As String
        str = "Source : " & ex.Source
        str &= ControlChars.NewLine
        str &= "Exception Message : " & ex.Message
        Console.WriteLine("Generic Exception" & str )
     Finally
        If SqlConnection1.State = ConnectionState.Open Then
           Console.WriteLine("Finally block closing the connection")
           SqlConnection1.Close()
        End If
     End Try
  End Sub

End Class


 </source>


SqlCommand.ExecuteScalar()

<source lang="vbnet"> 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


 </source>