Sql2005中對XML類型欄位的運用

來源:互聯網
上載者:User
sql2005|xml

在SQL2005中增加了xml類型資料。這樣,我們可以將我們應用程式中的實體物件直接儲存到資料庫中。下次要取的時候就可以直接將XML還原序列化成實體物件。對於資料量不是很大的情況下,可以考慮使用。

select * from Employee
 where [content].exist('//Age[text()>9000]')=1

此SQL語句中帶有xpath的查詢,可以找出employee表中content為XML類型列中子節點>9000的所有記錄

看一下,運用.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.IO;
namespace DevTest
{
    public partial class Form3 : Form
    {
        SqlConnection cn = new SqlConnection("server=192.168.1.100\\SqlExpress;uid=hpasc;pwd=9637004;database=filemanager");
        public Form3()
        {
            InitializeComponent();
            cn.Open();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = cn.GetSchema();
            this.dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = cn.GetSchema("Tables",null);
            this.dataGridView1.DataSource = dt;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //this.dataGridView1.DataSource = cn.GetSchema("IndexColumns", new string[] { "FileManager", "dbo", "Creers" });
            SqlCommand cmd = new SqlCommand();
            List<Employee> Emps = new List<Employee>();
            cmd.CommandText = "insert into Employee values(@OID,@Name,@Age,@City,@Address,@Birthday,@Content)";
            XmlSerializer ser = new XmlSerializer(typeof(Employee));
          
            cmd.Connection = cn;
            cmd.Prepare();
            this.textBox1.AppendText("產生對象時間" + System.DateTime.Now.ToString());
            for(int i=0;i<10000;i++)
            {
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                Employee emp=new Employee();
                emp.Address="emp.Address"+i.ToString();;
                emp.Age=i;
                emp.Name="Name"+i.ToString();
                emp.OID=Guid.NewGuid();
                emp.Birthday=DateTime.Now.AddHours(-i);
                emp.City="City"+i.ToString();
                ser.Serialize(ms,emp);
                ms.Position = 0;
               
                StreamReader sr = new StreamReader(ms);
                emp.Content = sr.ReadToEnd();
                sr.Close();
                Emps.Add(emp);
            }
            this.textBox1.AppendText("\r\n結束產生對象時間" + System.DateTime.Now.ToString());
            this.textBox1.AppendText("\r\n開始資料插入" + System.DateTime.Now.ToString());
            foreach(Employee emp in Emps)
            {
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@OID", SqlDbType.UniqueIdentifier).Value = emp.OID;
                cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50).Value = emp.Name;
                cmd.Parameters.Add("@City", SqlDbType.NVarChar, 50).Value = emp.City;
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar, 50).Value = emp.Address;
                cmd.Parameters.Add("@Age", SqlDbType.Int).Value = emp.Age;
                cmd.Parameters.Add("@Birthday", SqlDbType.DateTime).Value = emp.Birthday;
                cmd.Parameters.Add("@Content", SqlDbType.Xml).Value = emp.Content;
                cmd.ExecuteNonQuery();
            }
            this.textBox1.AppendText("\r\n結否資料插入" + System.DateTime.Now.ToString());
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
             List<Employee> Emps = new List<Employee>();
          
            XmlSerializer ser = new XmlSerializer(typeof(Employee));
            this.textBox1.AppendText("\r\n開始取出資料對象時間" + System.DateTime.Now.ToString());
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandText = "select Content from Employee";
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
          
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                MemoryStream ms = new MemoryStream();
                StreamWriter sr = new StreamWriter(ms);
                sr.WriteLine(dr[0].ToString());
                sr.Flush();
                ms.Position = 0;
                Employee emp=(Employee)ser.Deserialize(ms);
                Emps.Add(emp);
                sr.Close();
                //Employee emp=(Emp)
            }
            this.textBox1.AppendText("\r\n結束反序列產生對象時間" + System.DateTime.Now.ToString());
            this.dataGridView1.DataSource = Emps;
        }
    }

    [Serializable]
    public class Employee
    {
        Guid _OID;

        public Guid OID
        {
            get { return _OID; }
            set { _OID = value; }
        }

         string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        int _Age;

        public int Age
        {
            get { return _Age; }
            set { _Age = value; }
        }
      
        string _Address;

        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        DateTime _Birthday;

        public DateTime Birthday
        {
            get { return _Birthday; }
            set { _Birthday = value; }
        }
        string _City;

        public string City
        {
            get { return _City; }
            set { _City = value; }
        }
        string _Content;

        public string Content
        {
            get { return _Content; }
            set { _Content = value; }
        }

      
    
    }
}

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.