我已经将csv加载到数据表中,但是当它们不是空白时,其中一些列被读取为空白。我最初提出这个问题只是标题有问题,但我现在也在我的数据行中看到这个问题,所以我需要重新...我的数据集有什么问题,为什么有些列被读作一个空白?
目前,此设置将读取第1-7列的数据(我不需要8-10)。除了第4列之外,正确填充了所有列的数据。奇怪的是,我有两个我测试过的文件,它们的结构相似,但其中一个在第4列中有值而另一个没有。设置完整代码以循环遍历许多文件,检查数据的开始和结束,然后加载到sql server。
示例CSV:
By OrgID/Location
As of: December 6, 2017 at 10:13 AM
Date Range: summaryYM 2017M08 to 2017M08
"orgid=13778 medType=' '"
"col1","col2","col3","col4","col5","col6","col7","col8","col9","col10"
13778,140242,"2A","2017M08",0,0.058,78,".",".",
13778,140242,"2B","2017M08",0,0.014,19,".",".",
13778,140242,"2C","2017M08",0,0.083,133,".",".",
13778,140242,"2ICU","2017M08",0,0.099,114,".",".",
13778,140242,"3 ICU","2017M08",0,0.076,88,".",".",
码
//open connection to csv
string connStrCsv = string.Format(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=NO;FMT=Delimited"""
, Path.GetDirectoryName(file));
OleDbConnection connCsv = new OleDbConnection(connStrCsv);
connCsv.Open();
//store csv data in datatable
string readCsv = "select * from [" + Path.GetFileName(file) + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(readCsv, connCsv);
DataSet ds = new DataSet();
adapter.Fill(ds, "sheet1");
DataTable table = ds.Tables["sheet1"];
connCsv.Close();
//find header to define start of data
int start = 0;
StreamReader headerSearch = null;
int incr = 0;
headerSearch = new StreamReader(file);
while (!headerSearch.EndOfStream)
{
incr++;
string line = headerSearch.ReadLine();
if (line.Contains("\"col1\",\"col2\",\"col3\",\"col4\",\"col5\",\"col6\""))
{
start = incr;
}
}
headerSearch.Close();
//load each row of excel into SQL server until first empty row
string sqlConnStr = "Data Source=mysource;Initial Catalog=mydatabase;Trusted_Connection=Yes;Integrated Security=SSPI;";
SqlConnection connSql = new SqlConnection(sqlConnStr);
connSql.Open();
int end = start;
while (table.Rows[end][0].ToString().Length != 0)
{
string sql = string.Format
(@"
delete from schema.table
where ss_col1 = {0}
and ss_col2 = '{1}'
and ss_col3 = '{2}'
and ss_col4 = '{3}';
insert into schema.table
values ({4}
,'{5}'
,'{6}'
,'{7}'
, {8}
,'{9}'
,'{10}'
,getdate()
,user_name()
,getdate()
,user_name());"
//delete statement variables
, table.Rows[end][0].ToString()
, table.Rows[end][2].ToString()
, table.Rows[end][3].ToString()
, infTypes[i]
//insert statement variables
, table.Rows[end][0].ToString()
, table.Rows[end][2].ToString()
, table.Rows[end][3].ToString()
, infTypes[i]
, table.Rows[end][4]
, table.Rows[end][5].ToString()
, table.Rows[end][6]
);
SqlCommand execSql = new SqlCommand(sql, connSql);
execSql.ExecuteNonQuery();
end++;
}
connSql.Close();
答案 0 :(得分:1)
您是否必须将其加载到数据表中? 如果“header1”,“header2”,“header3”,“header4”,“header5”,“header6”都是唯一的,那么在你找到csv文件之前读取它们并不容易吗?
示例......
$( id + ' .attachedImage').load( $( id + ' .threadLink').attr('href') ...