SqlDataAdapter Problem

S

Steven Blair

Hi,

Here is a short decsription of my problem.

I have written a dll for Database accessing. I have one method which can
return a Dataset and another method which takes a Dataset and upates a
Database. (Disconnected Datasets).
The problem I have is a new SqlAdapter is required before updating the
Database:
Therefore, to allow the update I have to do the following:

m_DataAdaptor = new SqlAdapter("select * from club",m_Conn);

I want a generic method for updating, but this way means I have to put a
hardcoded SQL string as the first parameter. The connection object is a
class member and the DataSet is passed in from the client.

Basically, what I want is a method that takes a Dataset as a parameter and
can update a Database.

One idea I had was to make the SqlDataAdapter a method of the DB class. This
means the SqlDataAdapter would stay in scope during the life of the object
(DB class).

Is there some other method of updating a Database using a Dataset ?

Any help / suggestions would be appreciated

Regards,

Steven
www.stevenblair.com
 
S

Saso Zagoranski

the SqlDataAdapter has (among others) 4 properties:
SelectStatement, InsertStatement, UpdateStatement, DeleteStatement...

When you create the dataAdapter using the wizard you also select, which
table are you going to be working with...
Now when you call:
sqlDataAdapter.Fill(dataSet);
it calls it's SelectStatement and gives the dataSet a schema which is the
same as the selected columns in the table...

Now you edit the dataSet and you call
sqlDataAdapter.Update(dataSet);
the dataAdapter now calls it's UpdateStatement, which looks like "UPDATE
table SET col1 = 1 ... coln = N WHERE..."

I'm not sure it's possible to write a generic class...

I may be wrong...

The sql string doesn't have to be hardcoded... why not use column names as
parameters (or properties) and construct the sql query during runtime?

btw. you don't have to create a new DataAdapter each time... you can just
say:
DataAdapter.SelectStatement.CommandText = "....";

Saso
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top