Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Difference between SqlCommand and SqlCommandBuilder

Difference between SqlCommand and SqlCommandBuilder

Scheduled Pinned Locked Moved Database
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    param thaker
    wrote on last edited by
    #1

    Hi everybody, Can anybody tell me the exact difference between SqlCommand and SqlCommandBuilder. Thanking you.

    param

    K 1 Reply Last reply
    0
    • P param thaker

      Hi everybody, Can anybody tell me the exact difference between SqlCommand and SqlCommandBuilder. Thanking you.

      param

      K Offline
      K Offline
      Kschuler
      wrote on last edited by
      #2

      A Command object is used to execute scalar or non query commands to a database. You would set a command objects CommandText property to and sql statement that you want to run and then use it's ExecuteScalar or ExecuteNonQuery method to run it. Here is an example:

      Dim conn As New SqlClient.SqlConnection(strMyConnectionString)
      Dim cmd As New SqlClient.SqlCommand()
      cmd.CommandText = "UPDATE myTable SET col1='my value'"
      cmd.ExecuteNonQuery()

      A CommandBuilder object is used to automatically create Update, Delete, and Insert SQL statements for you, based on a Select statament that you supply. You would declare a DataAdapter object, set it's SelectCommand.CommandText property to your Select SQL statement. Then when you declare a CommandBuilder object, you include the dataadapter in the CommandBuilder's constructor parameter and it will automatically create the other statements for you when you run a DataAdapter. Here is an example:

      Dim conn As New SqlClient.SqlConnection(strMyConnectionString)
      Dim cmd As New SqlClient.SqlCommand()
      cmd.CommandText = "SELECT * FROM myTable"
      Dim da As New SqlClient.SqlDataAdapter(cmd)
      Dim cb As New SqlClient.SqlCommandBuilder(da)
      da.Update(myDataTable)

      If you hadn't of used a CommandBuilder, the program would have blown up on the da.Update(myDataTable) statement, because the adapter wouldn't have known how to insert new rows that had been added to the table, or delete rows that had been removed, etc.

      R 1 Reply Last reply
      0
      • K Kschuler

        A Command object is used to execute scalar or non query commands to a database. You would set a command objects CommandText property to and sql statement that you want to run and then use it's ExecuteScalar or ExecuteNonQuery method to run it. Here is an example:

        Dim conn As New SqlClient.SqlConnection(strMyConnectionString)
        Dim cmd As New SqlClient.SqlCommand()
        cmd.CommandText = "UPDATE myTable SET col1='my value'"
        cmd.ExecuteNonQuery()

        A CommandBuilder object is used to automatically create Update, Delete, and Insert SQL statements for you, based on a Select statament that you supply. You would declare a DataAdapter object, set it's SelectCommand.CommandText property to your Select SQL statement. Then when you declare a CommandBuilder object, you include the dataadapter in the CommandBuilder's constructor parameter and it will automatically create the other statements for you when you run a DataAdapter. Here is an example:

        Dim conn As New SqlClient.SqlConnection(strMyConnectionString)
        Dim cmd As New SqlClient.SqlCommand()
        cmd.CommandText = "SELECT * FROM myTable"
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim cb As New SqlClient.SqlCommandBuilder(da)
        da.Update(myDataTable)

        If you hadn't of used a CommandBuilder, the program would have blown up on the da.Update(myDataTable) statement, because the adapter wouldn't have known how to insert new rows that had been added to the table, or delete rows that had been removed, etc.

        R Offline
        R Offline
        Rob Graham
        wrote on last edited by
        #3

        An SQLCommand object can also be used to return query results. Under the covers, this is what happens in the DataAdapter - the command buider builds commandtext for the other sqlcommand instances used by the adapter, using the commandtext provided for the query or read command. SqlCommand instances can also be used to return query results as a datreader, which is like a fast forward-only cursor.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups