I try to use DataAdapter in C#.net. and still I do not understand something about DataAdapter.
I read many article and blogs about DataAdapter and DataReader.
I understood DataAdapter will open and close database automatically when they need.
But,
//conn.Open();
AdsDataAdapter da;
da = new AdsDataAdapter("Select * from Test", conn);
AdsCommandBuilder cb;
cb = new AdsCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "Test");
DataRow newrow = ds.Tables["Test"].NewRow();
newrow["Name"] = "How about";
ds.Tables["Test"].Rows.Add(newrow);
da.Update(ds, "Test");
When I run the code above, I get an error message that say "Connection must be open."
Why the adapter can not open connection automatically?
and, I want to insert data using insertCommand (For this test, I opened the connection).
da.InsertCommand = new AdsCommand("INSERT INTO test (NAME) values('Insert Test #1')", conn);
//da.InsertCommand.ExecuteNonQuery(); // it works
da.Update(ds,"Test"); //but it does not works.
Many example using Adapter.Update(), but for me, it does not work :(
No error and nothing inserted.
and using da.InsertCommand.ExecuteNonQuery(); instead Update(), it works.
what am I doing wrong?
Thanks!