110,567
社区成员
发帖
与我相关
我的任务
分享
for (int i = 0; i < 5; i++)
{
string[] files = list[i].ToArray<string>();
ThreadPool.QueueUserWorkItem(p =>
{
foreach (string file in files)
{
string[] temp = file.Split('\\');
string name = temp[temp.Length - 1].Replace(".csv", "");
int count = 0;
operateCSV.readCsvTxt(file, name, dataType, "Quotes", out count);
//MessageShow(name + " " + count.ToString() + " " + fileCount++);
}
});
}
/// <summary>
/// 读取CSV文件通过文本格式
/// </summary>
/// <param name="strpath"></param>
/// <returns></returns>
public static void readCsvTxt(string strpath, string code, string dataType, string tableName, out int count)
{
string strline;
string[] aryline;
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath);
string sqlStr = "Data Source=localhost;Initial Catalog=StockDataDB;Integrated Security=True";
//数据库
SqlConnection conn = new SqlConnection(sqlStr);
conn.Open();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
count = 0;
while ((strline = mysr.ReadLine()) != null)
{
aryline = strline.Split(',');
if(aryline[0] == "\"\"")
{
continue;
}
bool isNull = HasNull(aryline);
if (!isNull)
{
continue;
}
string Code = code;
string DataType = dataType;
DateTime dtime = Convert.ToDateTime(aryline[1]);
int Year = dtime.Year;
int Quarter = GetQuarterByMonth(dtime.Month);
int Month = dtime.Month;
int Week = WeekOfYear(dtime);
int Day = dtime.Day;
DateTime DATETIME = dtime;
double OPEN = Convert.ToDouble(aryline[2]);
double HIGH = Convert.ToDouble(aryline[3]);
double LOW = Convert.ToDouble(aryline[4]);
double CLOSE = Convert.ToDouble(aryline[5]);
double VOLUME = Convert.ToDouble(aryline[6]);
double Amount = Convert.ToDouble(aryline[7]);
//Data Source=KOBE;Initial Catalog=StockDataDB;Integrated Security=True
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO [" + tableName + "] ([Code],[DataType], [Year], [Quarter], [Month], [Week], [Day], [DATETIME], [OPEN], [HIGH], [LOW], [CLOSE], [VOLUME], [Amount]) VALUES (@Code,@DataType, @Year, @Quarter, @Month, @Week, @Day, @DATETIME, @OPEN, @HIGH, @LOW, @CLOSE, @VOLUME, @Amount)";
//添加参数
SqlParameter p = cmd.Parameters.Add("Code", SqlDbType.Char);
p.Value = Code;
p = cmd.Parameters.Add("DataType", SqlDbType.Char);
p.Value = DataType;
p = cmd.Parameters.Add("Year", SqlDbType.Int);
p.Value = Year;
p = cmd.Parameters.Add("Quarter", SqlDbType.Int);
p.Value = Quarter;
p = cmd.Parameters.Add("Month", SqlDbType.Int);
p.Value = Month;
p = cmd.Parameters.Add("Week", SqlDbType.Int);
p.Value = Week;
p = cmd.Parameters.Add("Day", SqlDbType.Int);
p.Value = Day;
p = cmd.Parameters.Add("DATETIME", SqlDbType.DateTime);
p.Value = DATETIME;
p = cmd.Parameters.Add("OPEN", SqlDbType.Float);
p.Value = OPEN;
p = cmd.Parameters.Add("HIGH", SqlDbType.Float);
p.Value = HIGH;
p = cmd.Parameters.Add("LOW", SqlDbType.Float);
p.Value = LOW;
p = cmd.Parameters.Add("CLOSE", SqlDbType.Float);
p.Value = CLOSE;
p = cmd.Parameters.Add("VOLUME", SqlDbType.Float);
p.Value = VOLUME;
p = cmd.Parameters.Add("Amount", SqlDbType.Float);
p.Value = Amount;
//执行
count += cmd.ExecuteNonQuery();
}
conn.Close();
mysr.Close();
}