-
Notifications
You must be signed in to change notification settings - Fork 145
Xml Parameters and Results
For the SQL Server provider, you can use the xml
data type as both input parameters and results. Insight automatically can convert various types to and from the xml
type.
Assume you have a stored procedure like this:
CREATE PROC InsightTestProc (@Xml xml) AS
-- do something
GO
XmlDocument is converted to an Xml parameter by sending the OuterXml to the server.
// create a document
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Data><Text>foo</Text></Data>");
_connection.Execute("InsightTestProc", new { Xml = doc });
XDocument is also converted to an Xml parameter by sending the OuterXml to the server.
XDocument doc = XDocument.Parse("<Data><Text>foo</Text></Data>");
_connection.Execute("InsightTestProc", new { Xml = doc });
Strings are sent unconverted to an Xml parameter. The caller is required to verify that the string can be converted to Xml by the server.
string doc = "<Data><Text>foo</Text></Data>";
_connection.Execute("InsightTestProc", new { Xml = doc });
Any other object is converted to an Xml parameter by using the DataContractSerializer.
class Data
{
public string Text;
}
// create a document
Data d = new Data()
{
Text = "foo"
};
_connection.Execute("InsightTestProc", new { Xml = d });
Yes, this works too.
Assume you have:
CREATE TYPE [XmlDataTable] AS TABLE ([Data] [Xml])
CREATE PROCEDURE [XmlTestProc] @p [XmlDataTable] READONLY AS SELECT * FROM @p
When Insight maps an object to XmlDataTable, it will automatically use the rules above to convert the property into Xml for the column.
So if you have an object that contains an object:
class Parent
{
public Data Data;
}
You can send a Parent to the database, and Data will automatically be converted to the Xml column using the DataContractSerializer.
Parent r = new Parent();
r.Data = new Data();
r.Data.Text = "foo";
var list = _connection.Query<Parent>("XmlTestProc", new { p = new List<Parent>() { r } });
var item = list[0];
Note that the query results also convert Xml columns back into objects, XDocuments, XmlDocuments or strings for you.
Really? Yes.
- Home
- About
- Getting Started
- Connections
- Execute
- Getting Results
- Advanced Results & Mapping
- Insert/Update Considerations
- Data Types
- Write Even Less Code
- Performance & Speed
- Other Topics
- Supported Databases
- Working with the Code