0
我正在VS 2008中開發一個C#web應用程序,該應用程序與我的SQL Server 2008中的Adventureworks數據庫進行交互。現在,我正在嘗試向其中包含XML列的其中一個表添加新記錄。我該怎麼做呢? 這是我得到的錯誤:如何將XML參數添加到C#中的存儲過程?
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參數:
'<Products><id>3</id><id>6</id><id>15</id></Products>'
這是XML的有效格式嗎?
謝謝,它現在看起來像是在爲我工作! – salvationishere 2010-04-15 02:12:22