c# - whats the use of data adapter -
can explain why sqldataadapter
used in following code? code working fine without adapter.
also, why use dataadapter
? please me understand dataadapter
usage.
namespace windowsformsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); private void button1_click(object sender, eventargs e) { try { sqlconnection con = new sqlconnection("data source=.....\\sqlexpress;initial catalog=......;integrated security=true"); con.open(); sqldataadapter da =new sqldataadapter(); // why use `sqldataadapter` here? sqlcommand sc = new sqlcommand("insert bhargavc values(" + textbox1.text + "," + textbox2.text + ");", con); var o = sc.executenonquery(); messagebox.show(o + "record inserted"); con.close(); } catch (exception) { messagebox.show("error in code"); } } private void button2_click(object sender, eventargs e) { application.exit(); } private void form1_load(object sender, eventargs e) { } } }
there several reasons use dataadapter:
- you can't fill dataset without one.
- when adapter completes
.fill()
method, close connection you; don't have explicitly call.close()
method on connection object. although, still practice to.
in case, isn't necessary have 1 though. but, if did want use one, implementation this:
sqldataadapter da = new sqldataadapter(); dataset ds = new dataset(); da.fill(ds);
from there, there further actions can take on ds
object exporting excel via interop, filling datagridview
or updating database table.
Comments
Post a Comment