SqlDataAdapter Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Blair

    #1

    SqlDataAdapter Problem

    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("sel ect * 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
    A blog about computational biology, axolotls, and black cats.



  • Saso Zagoranski

    #2
    Re: SqlDataAdapter Problem

    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.Sel ectStatement.Co mmandText = "....";

    Saso

    "Steven Blair" <steven.blair@b tinternet.com> wrote in message
    news:bo2ruj$kss $1@sparta.btint ernet.com...[color=blue]
    > 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("sel ect * 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.[/color]
    This[color=blue]
    > 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
    >
    >[/color]


    Comment

    Working...