C# CSV 导入数据库

Kevin_Jay_24 Kevin_Jay_24 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
做个导入CSV到MSSQL数据库的工具,数据每行都要处理验证,使用的线程池,但是发现只有一个线程的效果,不明为何,求大神指点
这是button点击事件
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();

}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
Kevin_Jay_24
C# 111,128 成员 642,536 内容