它确实有效。您必须将值设置为 SqlXml 而不是字符串,但可以这样做。想象一下这张桌子:
CREATE TABLE XmlTest
(
[XmlTestId] [int] identity(1,1) primary key,
[XmlText] [xml] NOT NULL
)
和存储过程:
CREATE PROCEDURE XmlTest_Insert
(
@XmlText xml
)
AS
INSERT INTO XmlTest (XmlText)
VALUES (@XmlText)
现在想象一个如下所示的控制台应用程序:
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;
using System.Xml;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
string xmlDoc = "<root><el1>Nothing</el1></root>";
string connString = "server=(local);database=IntroDB;UID=sa;PWD=pwd";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("XmlTest_Insert", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@XmlText", SqlDbType.Xml);
param.Value = new SqlXml(new XmlTextReader(xmlDoc
, XmlNodeType.Document, null));
cmd.Parameters.Add(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Dispose();
}
}
}
答对了!
这是在 Visual Studio 2008 (.NET 3.5) 中完成的,但我相当肯定它也应该在 Visual Studio 2005 (2.0 Framework) 中工作。