本帖最后由 txh2002 于 2015-4-8 11:05 编辑
namespace expzpdata
{
public delegate void DelegateSetProgress(int i);
public partial class Form1 : Form
{
public event DelegateSetProgress SetProgress;
public event DelegateSetProgress SetComplet;
Thread th1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
fd.SelectedPath = @"c:";
if (fd.ShowDialog() == DialogResult.OK)
{
textBox3.Text = fd.SelectedPath;
}
textBox3.Text = fd.SelectedPath;
}
private void button2_Click(object sender, EventArgs e)
{
this.SetProgress+=new DelegateSetProgress(Form1_SetProgress);
this.SetComplet += new DelegateSetProgress(Form1_SetComplet);
th1 = new Thread(expdata);
th1.Start();
button2.Enabled = false;
}
void Form1_SetComplet(int i)
{
label4.Text = "导出行数: " + i.ToString();
MessageBox.Show("完成导出数据。");
button2.Enabled = true;
throw new NotImplementedException();
}
void Form1_SetProgress(int i)
{
label4.Text = "导出行数: " + i.ToString();
throw new NotImplementedException();
}
private void expdata()
{
OracleConnection con = new OracleConnection();
string str = @"Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.166.71.2)(PORT=1521))(CONNECT_DATA=(sid=pzhjhk2)));User Id=csi_pzhassword=csi_pzh";
con.ConnectionString=str;
string sql="select aac002,aac003,aac034 from tmp_zp"; //aac002身份证 aac003姓名 aac034照片
OracleDataReader dr;
OracleCommand cmd=new OracleCommand(sql,con);
con.Open();
dr=cmd.ExecuteReader();
FileStream fs = new FileStream(textBox3.Text + "\\" + "zp.xml",FileMode.OpenOrCreate);
StreamWriter sw=new StreamWriter(fs);
StringBuilder sb = new StringBuilder();
sb.Append(@"<?xml version='1.0' encoding='UTF-8'?>");
sb.Append("<root>");
int i = 0;
while (dr.Read())
{
sb.Append("<aac001>");
sb.Append("<aac002>");
sb.Append(dr[0]);
sb.Append("</aac002>");
sb.Append("<aac003>");
sb.Append(dr[1]);
sb.Append("</aac003>");
sb.Append("<aac034>");
byte[] data =(byte[])dr[2];
/* 把读出的blob解码后得到的是一个xml格式字符串,这里再次处理xml文档,只保留xml内部值。 */
string s = Encoding.UTF8.GetString(data);
XmlDocument xmldoc_t = new XmlDocument();
xmldoc_t.LoadXml(s);
XmlNodeList nodlist = xmldoc_t.GetElementsByTagName("file");
foreach (XmlNode node in nodlist)
{
sb.Append(node.InnerText);
}
sb.Append("</aac034>");
sb.Append("</aac001>");
i++; //循环计数,每100行写入一次文件。
if (i % 100 == 0)
{
sw.Write(sb.ToString());
sw.Flush();
sb = new StringBuilder();
SetProgress(i);
//label4.Text = "导出行数: " + i.ToString();
}
}
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
fs.Close();
//label4.Text = "导出行数: " + i.ToString();
SetComplet(i);
// MessageBox.Show("完成");
}
}
|