当前位置:  开发笔记 > 编程语言 > 正文

如何在C#中向存储过程添加XML参数?

如何解决《如何在C#中向存储过程添加XML参数?》经验,为你挑选了1个好方法。

我正在VS 2008中开发一个C#Web应用程序,它与我的SQL Server 2008中的Adventureworks数据库进行交互.现在我正在尝试将新记录添加到其中一个包含XML列的表中.我该怎么做呢?这是我得到的错误:

System.Data.SqlClient.SqlException was caught
  Message="XML Validation: Text node is not allowed at this location, the type was defined with element only content or with simple content. Location: /"
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060
  Class=16
  LineNumber=22
  Number=6909
  Procedure="AppendDataC"
  Server="."
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at ADONET_namespace.ADONET_methods.AppendDataC(DataRow d, Hashtable ht) in C:\Documents and Settings\Admin\My Documents\Visual Studio 2008\Projects\AddFileToSQL\AddFileToSQL\ADONET methods.cs:line 212
  InnerException: 

这是我在C#中的一部分代码:

    try
            {
                SqlConnection conn2 = new SqlConnection(connString);
                SqlCommand cmd = conn2.CreateCommand();
                cmd.CommandText = "dbo.AppendDataC";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn2;
...
                sqlParam10.SqlDbType = SqlDbType.VarChar;
                SqlParameter sqlParam11 = cmd.Parameters.AddWithValue("@" + ht["@col11"], d[10]);
                sqlParam11.SqlDbType = SqlDbType.VarChar;
                SqlParameter sqlParam12 = cmd.Parameters.AddWithValue("@" + ht["@col12"], d[11]);
                sqlParam12.SqlDbType = SqlDbType.Xml;
...
                conn2.Open();
                cmd.ExecuteNonQuery(); //This is the line it fails on and then jumps
                                       //to the Catch statement
                conn2.Close();
                errorMsg = "The Person.Contact table was successfully updated!";
            }
            catch (Exception ex)
            {

现在在我的文本输入MDF文件中我有XML参数:

'3615'

这是XML的有效格式吗?



1> Ben M..:

指定时SqlDbType.Xml,参数的值必须是SqlXml类的实例:

sqlParam12.Value = new SqlXml(...);

如果要将XML字符串设置为XML列(不进行实例化SqlXml),可以将参数类型设置为SqlDbType.VarChar- 在调用时实际上不需要AddWithValue- 并且它将被隐式转换,假设XML是格式良好(你的).

推荐阅读
农大军乐团_697
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有