Webmethod to save data to SQLServer

Discussion in 'ASP .Net Web Services' started by Rob, Jan 16, 2007.

  1. Rob

    Rob Guest

    I have a Webservice written in C# that provides data to both a web browser
    through ASP.net and to a VC++ client. I have been able to retrieve data
    from SQLServer and passing to my C++ client as XML without a problem using
    the SqlDataAdaptor and the DataSet. I am a bit confused how to save data to
    SQL Server from my client.

    I figured if I could create a DataSet, I could save it to the database. I
    wrote a WebMethod that takes a string (the XML) as input, reads that string
    using an XMLReader, then creates a DataSet from that reader.

    [WebMethod]
    public Boolean SaveStaff(string sStaffXML)
    {
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ConformanceLevel = ConformanceLevel.Fragment;
    settings.IgnoreWhitespace = true;
    settings.IgnoreComments = true;
    XmlReader reader = XmlReader.Create(sStaffXML, settings);

    // Create a new DataSet.
    DataSet staffDataSet = new DataSet("Staff DataSet");

    // Read the XML document into the DataSet.
    staffDataSet.ReadXml(reader);

    // Close the XmlTextReader
    reader.Close();

    SqlDataAdapter dataAdapter;

    SqlConnection conn;
    conn = new SqlConnection("Server
    =localhost;uid=stmUser;pwd=mypw;database=STMDB");

    string cmdString = "SELECT * FROM Staff"; // NOT SURE IF I NEED
    THIS!

    dataAdapter = new SqlDataAdapter(cmdString, conn);

    dataAdapter.Update(staffDataSet);

    return true;
    }

    Is this the right approach?

    I also wasn't sure exactly how to build the XML string so that I can pass it
    into the DataReader and then let .NET do its magic. Do I need to use an XSD
    file? Can I just emulate the XML that .NET passes to the client when it
    calls a data retrieval method that returns XML with Staff records in it?

    Are there any good articles about how to do this. most of the stuff out
    there about using web services use examples of retrieving data, not saving
    it.

    Regards,

    -Rob
    Rob, Jan 16, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristofer Liljeblad
    Replies:
    0
    Views:
    590
    Kristofer Liljeblad
    Jun 30, 2003
  2. D. Shane Fowlkes
    Replies:
    2
    Views:
    480
    D. Shane Fowlkes
    Feb 18, 2004
  3. =?Utf-8?B?SmVmZnJleQ==?=

    Upgrading ASP w/SQLserver 2000 to ASP.NET w/ SQLserver 2005

    =?Utf-8?B?SmVmZnJleQ==?=, Apr 6, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    465
    sloan
    Apr 27, 2007
  4. PA

    Best way to send data to webmethod

    PA, May 7, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    209
    Dino Chiesa [Microsoft]
    May 13, 2004
  5. Enda Mannion

    best way to publish webmethod parameters and return data

    Enda Mannion, Jun 15, 2009, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    757
    Enda Mannion
    Jun 15, 2009
Loading...

Share This Page