Simple question

  • Thread starter Thread starter divisortheory
  • Start date Start date
D

divisortheory

I have a table that I want to run a query on, and I'd ideally like a
"live" view of the data, so that I can add, delete, or modify rows, and
that stuff shows up in the original table. Some more details about
what I want to do:

1) Data is only coming from one table
2) I might modify rows that are returned to me, as well as delete some
rows, or add new rows. I want all of this to be reflected into the
original table.
3) I don't necessarily need it to be live, perhaps a transaction based
thing where I modify everything locally and then just call some
function like dataThingy.Accept()

What is the easiest way to do this? I've been trying to dig through
the help files but there's so many classes and they're so confusing.
DataReader, DataAdapter, DataTable, DataSet, DataView. I don't know
what I need, but it seems like what I want to do should be really
simple in theory.

I appreciate any advice.

Sincerely,
Zachary Turner
 
Hi,

In short words, create a dataadapter for the table in question (perhaps by
drag and drop a table from server explorer on the form), fill the DataTable
using that adapter and a valid connection.
When you are done changing the data in DataTable, use the same adapter to
store data back in database.
Make sure that your table has a primary key.
 
What you are looking for is to use transactions. Whichever data provider you
are using, you should have a BeginTransaction on the Connection object for
that data provider (eg. BeginTransaction method off OleDbConnection,
SqlConnection, etc). This returns the respective Transaction object which
you can then use to either Commit (Commit Method) or Rollback (Rollback
method) changes you've made. Look in the documentation for the appropriate
connection and transaction classes. You could use the respective Command
Object (OleDbCommand, SqlCommand, etc) object to build and execute the sql
you want to execute. Also take a look at the respective DataReader classes
(OleDbDataReader, SqlDataReader, etc) to read data from you SELECT
statements. Here's an article that might help"

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/transact.asp


hope that helps..
Imran.
 
Easiest:

1. Fill a DataSet with a DataAdapter with a SELECT Command object
2. Allow user to alter data
3. Re-hook to the DataAdapter and call Update() on the DataAdapter (you will
need to either create the UPDATE, INSERT and DELETE commands or use a
CommandBuilder to do this).
4. Handle any concurrency issues (data changed while you were editing).

Dino Esposito's columns in MSDN magazine (available online) have info on
this process and there is a lot of sample downloadable code (normally against
the Northwind sample database). His ASP.NET book is also a good source.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
I got to the point where I filled in the table, modified it, and am
ready to write it back. How do I write it back? What I did was:
DataTable table = new DataTable("Synths");

Adapter.Fill(table);
table.Rows[0].BeginEdit();
table.Rows[0].ItemArray[0] = NumVariations;
table.Rows[0].ItemArray[1] = CrystalName;
table.Rows[0].EndEdit();
table.AcceptChanges();
Adapter.Update(table);

but when I refresh the table in Access, nothing is different even
though I verified that both NumVariations and CrystalName are different
than what is in the array before I set them. I suspect that maybe I
need to set the .UpdateCommand of the Adapter object but is it a
regular sql UPDATE command? If so how do I write it so that it knows
to pull values from the table?

Thanks again
 
Hmmm... It seems like when I write the new value to the DataTable, it
still retains the old value, even though I very clearly set it to
something different. I can even change the value in the debugger
Window, like I can do a watch on table.Rows[0].ItemArray[1] and change
it to something totally different than it was before and the value
doesn't change. No matter what I do it won't change. I guess if I can
get that working I'd be done.

Zach
 
Hi,

There is no need and you shouldn't call AcceptChanges in your code since it
consolidates changes made to table.
Thus Update won't find any modifications and won't do a thing.
 
Back
Top