Webmethod to save data to SQLServer

R

Rob

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top