Overview : DataTable을 serialize할 수 없습니다. DataTable 이름이 설정되지 않았습니다
DataSet 과 달리 TableName 이름을 반드시 준다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
System.InvalidOperationException: XML 문서를 생성하는 동안 오류가 발생했습니다. ---> System.InvalidOperationException: DataTable을 serialize할 수 없습니다. DataTable 이름이 설정되지 않았습니다. 위치: System.Data.DataTable.WriteXmlSchema(XmlWriter writer, Boolean writeHierarchy) 위치: System.Data.DataTable.System.Xml.Serialization.IXmlSerializable.WriteXml(XmlWriter writer) 위치: System.Xml.Serialization.XmlSerializationWriter.WriteSerializable(IXmlSerializable serializable, String name, String ns, Boolean isNullable, Boolean wrapped) 위치: Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_DataTable(Object o) 위치: Microsoft.Xml.Serialization.GeneratedAssembly.DataTableSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer) 위치: System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) --- 내부 예외 스택 추적의 끝 --- 위치: System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) 위치: System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces) 위치: System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue) 위치: System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) 위치: System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) 위치: System.Web.Services.Protocols.WebServiceHandler.Invoke() |
1 2 3 |
ds.TableName = _key; //* 반드시 명시 한다. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Web; using System.Data; using System.Data.SqlClient; namespace WebService1 { public class CacheClass { private static string _key = "Customers"; private static string connectionString = "Data Source=localhost;" + "Initial Catalog=Northwind;user ID=sa"; public static DataTable GetCustomerDataSet() { if (HttpContext.Current.Cache[_key] as DataTable == null) { // Recreate the item. string SQL = "SELECT * FROM Customers"; // Create ADO.NET objects. SqlConnection con = new SqlConnection(connectionString); SqlCommand com = new SqlCommand(SQL, con); SqlDataAdapter adapter = new SqlDataAdapter(com); DataTable ds = new DataTable(); ds.TableName = _key; //* 반드시 명시 한다. try { con.Open(); adapter.Fill(ds); HttpContext.Current.Cache.Insert("Customers", ds, null, DateTime.Now.AddMinutes(60), TimeSpan.Zero); } catch (Exception err) { System.Diagnostics.Debug.WriteLine(err.ToString()); } finally { con.Close(); } } return HttpContext.Current.Cache[_key] as DataTable; } } } |