-- 作者:omeili
-- 发布时间:5/24/2010 3:07:00 PM
-- 帮忙啊,没办法了!xml与sql
想从sql从导出xml文件,使用VISUAL .NET2003编写的代码,不知道错在哪里了!代码测试无法通过,数据库已建立,并且在查询工具中可以正常查询。 请教各位高手哪个地方需要修改? using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using MSXML2; using ADODB; namespace example_8._1 { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; ADODB._Connection pConn; ADODB._Recordset pRS; String pSelStr; String pUser; String pPSW; String sname; String dbname; IXMLDOMDocument xmlDoc; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(128, 48); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(296, 208); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(152, 304); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(72, 64); this.button1.TabIndex = 1; this.button1.Text = "从数据库表生成XML文档"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(328, 304); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(72, 64); this.button2.TabIndex = 2; this.button2.Text = "显示生成的XML文档"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(584, 445); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "从数据库表生成XML文档"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { if(this.Connect2DB()) { xmlDoc=new DOMDocument40Class(); IXMLDOMProcessingInstruction pPI; pPI=xmlDoc.createProcessingInstruction("xml","version='1.0' encoding='GB2312' standalone='no'"); xmlDoc.appendChild(pPI as IXMLDOMNode); IXMLDOMComment pComment; pComment=xmlDoc.createComment("下面是实例"); xmlDoc.appendChild(pComment as IXMLDOMNode); IXMLDOMElement pElement; IXMLDOMElement pElementChild; IXMLDOMText pText; pElement=xmlDoc.createElement("students"); xmlDoc.appendChild(pElement as IXMLDOMNode); int count; pRS.MoveFirst(); while(pRS.EOF==false) { count=0; pElement=xmlDoc.createElement("student"); foreach(ADODB.Field pField in pRS.Fields) { pElementChild=xmlDoc.createElement(pField.Name); pText=xmlDoc.createTextNode(pRS.get_Collect(count).ToString()); pElementChild.appendChild(pText as IXMLDOMNode); count++; pElement.appendChild(pElementChild as IXMLDOMNode); // MessageBox.Show(pField.Name); } xmlDoc.documentElement.appendChild(pElement as IXMLDOMNode); pRS.MoveNext(); } xmlDoc.save(Application.StartupPath+"//"+"test.xml"); MessageBox.Show("生成成功!"); pRS.Close(); pConn.Close(); } } private bool Connect2DB() { try { pConn=new ConnectionClass(); pRS=new ADODB.RecordsetClass(); dbname="students"; sname="aubs123"+"\\"+"JT"; pUser="sa"; pPSW="sa"; pSelStr="SELECT * From students"; pConn.Open("Provider=SQLOLEDB.1;Server=" + sname + ";Initial Catalog=" + dbname,pUser,pPSW,-1); pRS.Open(pSelStr,pConn,ADODB.CursorTypeEnum.adOpenDynamic,ADODB.LockTypeEnum.adLockOptimistic,-1); return true; } catch { return false; } } private void button2_Click(object sender, System.EventArgs e) { this.textBox1.Text=xmlDoc.xml; } } }
|