SQl And ADo.net Interview Questions

Posted by Senthilns2005 under Interview Questions on 6/3/2011 | Points: 10 | Views : 1635 | Status : [Member] | Replies : 4
What is difference between sqlcommand vs comand builder in ado.net

Regards
N.Senthilkumar



Responses

Posted by: Lakn2 on: 6/3/2011 [Member] Starter | Points: 25

Up
0
Down
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.


Thanks&Regards
LakshmiNarayana Nalluri.

Senthilns2005, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A4u6178 on: 6/3/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Refer the following resolved post at dotnetfunda..
http://www.dotnetfunda.com/forums/thread4549-why-sqlcommandbuilder-is-used-.aspx

Thanks & Regards;
aaa

Senthilns2005, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Senthilns2005 on: 6/3/2011 [Member] Starter | Points: 25

Up
0
Down
Thank you LakshmiNarayana very useful to me

Regards
N.Senthilkumar

Senthilns2005, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 6/3/2011 [Member] [MVP] Bronze | Points: 25

Login to post response