判断数据有问题try捕获跳过

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder("Data Source=.;Initial Catalog=master;User ID=sa;Password=dinglin");

        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboBox2.Items.Clear();
            this.comboBox2.Text = "";
            scsb.InitialCatalog = this.comboBox1.SelectedItem.ToString();

            using (SqlConnection conn = new SqlConnection(scsb.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("select name from sys.tables where type = 'u' order by name asc", conn))
                {
                    conn.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            this.comboBox2.Items.Add(sdr[0].ToString());
                        }
                    }
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(scsb.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("select name from sys.databases where database_id > 4 order by name asc", conn))
                {
                    conn.Open();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            this.comboBox1.Items.Add(sdr[0].ToString());
                        }
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string selectedFolderPath = this.folderBrowserDialog1.SelectedPath;
                this.textBox1.Text = selectedFolderPath + "\\";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string folderPath = this.textBox1.Text;
            string[] fileNames = Directory.GetFiles(folderPath);
            scsb.InitialCatalog = this.comboBox1.SelectedItem.ToString();
            using (SqlConnection conn = new SqlConnection(scsb.ConnectionString))
            {
                conn.Open();
                foreach (string fileName in fileNames)
                {
                    string name = Path.GetFileName(fileName);
                    Console.WriteLine(name);
                    using (DataTable dt = new DataTable(this.comboBox2.Text))
                    {
                        dt.Columns.Add("id", typeof(int));
                        dt.Columns.Add("InstrumentID", typeof(string));
                        dt.Columns.Add("TradingDay", typeof(DateTime));
                        dt.Columns.Add("UpdateTime", typeof(TimeSpan));
                        dt.Columns.Add("LastPrice", typeof(decimal));
                        dt.Columns.Add("PreClosePrice", typeof(decimal));
                        dt.Columns.Add("OpenPrice", typeof(decimal));
                        dt.Columns.Add("HighestPrice", typeof(decimal));
                        dt.Columns.Add("LowestPrice", typeof(decimal));
                        dt.Columns.Add("Volume", typeof(int));
                        dt.Columns.Add("LowerLimitPrice", typeof(Decimal));


                        using (StreamReader sr = new StreamReader(folderPath + name))
                        {
                            string line = sr.ReadLine().Skip(1).ToString();
                            while ((line = sr.ReadLine()) != null)
                            {
                                try
                                {
                                    string[] fields = line.Split(',');
                                    if (int.Parse(fields[11].ToString()) == 0 && fields[0].ToString().Equals(""))
                                    {
                                        //    Console.WriteLine(int.Parse(fields[11].ToString()) == 0);
                                        //    Console.WriteLine(fields[0].ToString().Equals(""));
                                        continue;
                                    }
                                    DataRow dr = dt.NewRow();
                                    dr["InstrumentID"] = fields[1];
                                    dr["TradingDay"] = DateTime.ParseExact(fields[0], "yyyyMMdd", null).ToString("yyyy/MM/dd");
                                    //Console.WriteLine(dr["TradingDay"].ToString());
                                    dr["UpdateTime"] = fields[20].ToString();
                                    dr["LastPrice"] = fields[4];
                                    dr["PreClosePrice"] = fields[6];
                                    dr["OpenPrice"] = fields[8];
                                    dr["HighestPrice"] = fields[9];
                                    dr["LowestPrice"] = fields[10];
                                    dr["Volume"] = fields[11];
                                    dr["LowerLimitPrice"] = fields[17];
                                    dt.Rows.Add(dr);
                                }
                                catch (Exception)
                                {
                                    continue;
                                }

                            }
                            using (SqlBulkCopy sbc = new SqlBulkCopy(conn))
                            {
                                sbc.DestinationTableName = this.comboBox2.Text;
                                sbc.WriteToServer(dt);
                            }

                        }
                    }
                }
            }
            MessageBox.Show("完成");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值