By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,965 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,965 IT Pros & Developers. It's quick & easy.

Webmethod to save data to SQLServer

P: n/a
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
Jan 16 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.