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("完成");
}
}
}