static void InsertA(int aid, string contentXml) { //ConnString是连接字符串,需要额外定义 using (SqlConnection conn = new SqlConnection(ConnString)) { conn.Open(); string sql = "INSERT INTO [A] ([ID],[Content])VALUES(@id,@content)"; using (SqlCommand comm = new SqlCommand(sql, conn)) { using (XmlTextReader rdr = new XmlTextReader(contentXml, XmlNodeType.Document, null)) { SqlXml sqlXml = new SqlXml(rdr);
SqlParameter parmID = new SqlParameter("@id", aid); SqlParameter parmContent = new SqlParameter("@content", SqlDbType.Xml, sqlXml.Value.Length); parmContent.Value = sqlXml;
string GetContent(int id) { string sql = "SELECT [Content] FROM [A] WHERE [ID] = " + id;
using (SqlConnection conn = new SqlConnection(ConnString)) { conn.Open(); using (SqlCommand comm = new SqlCommand(sql,conn)) { string xml = (string)comm.ExecuteScalar(); return xml; } } }