• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
笨笨笨笨
博客园    首页    新随笔    联系   管理    订阅  订阅

之前所能记下的内容

在申请此博客之前,的确忘记了很多内容,现在将记住的放到下面:
以下是我将PBC数据库导为XML(button1)再将XML导入到EWS数据库(button2)的代码:

 


  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Text;
  7using System.Windows.Forms;
  8using System.Xml;
  9using System.Data.SqlClient;
 10using System.IO;
 11
 12namespace mothconvert
 13{
 14    public partial class Form1 : Form
 15    {
 16        public Form1()
 17        {
 18            InitializeComponent();
 19        }

 20
 21        private XmlDocument doc = new XmlDocument();
 22
 23        private void button1_Click(object sender, EventArgs e)
 24        {
 25            listBox1.SelectedIndexChanged += new EventHandler(example1_SelectedIndexChanged);
 26            DataSet dsPBC = new DataSet("XMLMonthBase");
 27            SqlConnection conn = new SqlConnection("server=localhost;Integrated Security=SSPI;database=PBCDataBaseTest;");
 28            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM MonthBase", conn);
 29
 30            MemoryStream memStrm = new MemoryStream();
 31            StreamReader strmRead = new StreamReader(memStrm);
 32            StreamWriter strmWrite = new StreamWriter(memStrm);
 33
 34            da.Fill(dsPBC, "MonthBase");
 35            dataGridView1.DataSource = dsPBC.Tables[0];
 36
 37            dsPBC.WriteXml(strmWrite, XmlWriteMode.IgnoreSchema);
 38            memStrm.Seek(0, SeekOrigin.Begin);
 39
 40            doc.Load(strmRead);
 41            XmlNodeList nodeLst = doc.SelectNodes("//UID");
 42            foreach (XmlNode nd in nodeLst)
 43                listBox1.Items.Add(nd.InnerText);
 44
 45            string file = "C:\\Documents and Settings\\dell\\桌面http://www.cnblogs.com/xuben/admin/file://pbcmonth.xml/";
 46            dsPBC.WriteXml(file);
 47        }

 48
 49        void example1_SelectedIndexChanged(object sender, EventArgs e)
 50        {
 51            string srch = "XMLMonthBase/MonthBase[UID=" + " " +
 52                           listBox1.SelectedItem.ToString() + " " + "]";
 53            XmlNode foundNode = doc.SelectSingleNode(srch);
 54
 55            if (foundNode != null)
 56                MessageBox.Show("foundNode.OuterXml");
 57            else
 58                MessageBox.Show("not found");
 59        }

 60
 61        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
 62        {
 63
 64        }

 65
 66        private void button2_Click(object sender, EventArgs e)
 67        {
 68            string myXMLfile = @"C:\\Documents and Settings\\dell\\桌面http://www.cnblogs.com/xuben/admin/file://pbcmonth.xml/";
 69            DataSet dsPBC = new DataSet();
 70            System.IO.FileStream fsReadXml = new System.IO.FileStream(myXMLfile, System.IO.FileMode.Open);
 71
 72            dsPBC.ReadXml(fsReadXml);
 73            dataGridView1.DataSource = dsPBC.Tables[0];
 74
 75            //tableName = PBCmonth.xml.Substring(0,PBCmonth.xml.Length);
 76
 77            SqlConnection conn = new SqlConnection("server=localhost;Integrated Security=SSPI;database=EWSDataBase_Test;");
 78
 79            SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("select IndicatorCode,IndicatorID from IndicatorBasicInfo", conn);
 80            DataSet dsBasicInfo = new DataSet();
 81            mySqlDataAdapter.Fill(dsBasicInfo);
 82
 83            string insertSqlMonthBase = " INSERT INTO IndicatorMonthBase (DateString,IndicatorValue,IndicatorID,CreatedBy,IndicatorDataTypeID,ProviderID) VALUES ('{0}',{1},'{2}',1,1,1) ";
 84            string insertSqlBasicInfo = " INSERT INTO IndicatorBasicInfo (IndicatorCode,IndicatorGroupID,DepartmentID) VALUES ('{0}','{1}',1) ";
 85            string selectSqlMonthBase = " SELECT @IndicatorID FROM IndicatorBasicInfo WHERE @IndicatorID = IndicatorCode ";
 86            //string selectSqlBasicInfo = " SELECT * FROM dsPBC WHERE @IndicatorID not in(SELECT @IndicatorCode FROM IndicatorBasicInfo)";
 87
 88            SqlCommand InsertBasicInfoCommand = new SqlCommand(insertSqlBasicInfo, conn);
 89            SqlCommand InsertMonthBaseCommand = new SqlCommand(insertSqlMonthBase, conn);
 90            SqlCommand SelectMonthBaseCommand = new SqlCommand(selectSqlMonthBase, conn);
 91            // SqlCommand SelectBasicInfoCommand = new SqlCommand(selectSqlBasicInfo, conn);
 92            conn.Open();
 93
 94            bool ExistBasicinfo = true;
 95            if (dsPBC.Tables[0].Rows.Count > 0)
 96            {
 97                for (int i = 0; i < dsPBC.Tables[0].Rows.Count; i++)
 98                {
 99                    DataRow dr = dsPBC.Tables[0].Rows[i];
100                    string DateString;
101
102                    /**////合并年-月值
103                    if (int.Parse(dr["MonthNo"].ToString()) < 10)
104                    {
105                        DateString = dr["YearNo"].ToString() + "-0" + dr["MonthNo"].ToString();
106                    }

107                    else
108                    {
109                        DateString = dr["YearNo"].ToString() + "-" + dr["MonthNo"].ToString();
110                    }

111
112                    double IndicatorValue = double.Parse(dr["IndicatorValue"].ToString());
113
114                    /**////判断dsBasicInfo的与dsPBC是否相等,若相等,则将ExistBasicinfo置为false,跳出循环;
115                    ///否则将ExistBasicinfo置为true,继续循环

116                    for (int j = 0; j < dsBasicInfo.Tables[0].Rows.Count; j++)
117                    {
118                        if (dsPBC.Tables[0].Rows[i]["IndicatorID"].ToString() == dsBasicInfo.Tables[0].Rows[j]["IndicatorCode"].ToString())
119                        {
120                            ExistBasicinfo = false;
121                            break;
122                        }

123
124                        else
125                            ExistBasicinfo = true;
126                            continue;
127                    }

128
129                    /**////若ExistBasicinfo为true,表示dsBasicInfo无dsPBC的ID,则将此ID作为Code插入dsBasicInfo
130                    if (ExistBasicinfo == true )
131                    {
132                        // SelectBasicInfoCommand.CommandText = string.Format(selectSqlBasicInfo);
133                        string IndicatorCode = "A05";
134                        string IndicatorGroupID = "A";
135
136                        IndicatorCode = dsPBC.Tables[0].Rows[i]["IndicatorID"].ToString();
137                        InsertBasicInfoCommand.CommandText = string.Format(insertSqlBasicInfo, IndicatorCode, IndicatorGroupID);
138                        InsertBasicInfoCommand.ExecuteNonQuery();
139                    }

140
141                    /**////再次判断dsBasicInfo与dsPBC是否相等,若相等,则将dsPBC此项插入到MonthBase数据表中
142                    for (int k = 0; k < dsBasicInfo.Tables[0].Rows.Count; k++)
143                    {
144                        int IndicatorID = 0;
145                        if (dsPBC.Tables[0].Rows[i]["IndicatorID"].ToString() == dsBasicInfo.Tables[0].Rows[k]["IndicatorCode"].ToString())
146                        {
147                            IndicatorID = int.Parse(dsBasicInfo.Tables[0].Rows[k]["IndicatorID"].ToString());
148                            SelectMonthBaseCommand.CommandText = string.Format(selectSqlMonthBase);
149                            InsertMonthBaseCommand.CommandText = string.Format(insertSqlMonthBase, DateString, IndicatorValue, IndicatorID);
150                            InsertMonthBaseCommand.ExecuteNonQuery();
151                            break;
152                        }

153
154                        else continue;
155                    }

156                }

157            }

158
159            conn.Close();
160
161        }

162
163    }

164}

写得非常凌乱,但就我现在的水平,并不知道怎么弄得更清晰,只能先实现功能,日后再一步一步的来了!~

posted @ 2008-07-17 14:56  笨笨笨笨  阅读(141)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3