C# 用C语言解压表格#
我正在C#中构建一个函数,用于在CSV文件中解压一个复杂的表,并将其插入到SQL表中。该文件如下所示:C# 用C语言解压表格#,c#,C#,我正在C#中构建一个函数,用于在CSV文件中解压一个复杂的表,并将其插入到SQL表中。该文件如下所示: | 1/5/2018 | 1/5/2018 | 1/6/2018 | 1/6/2018... City: | min: | max: | min: | max: Boston(KBOS) | 1 | 10 | 5 | 12 我的目标是这样解压它: airport_code | localt
| 1/5/2018 | 1/5/2018 | 1/6/2018 | 1/6/2018...
City: | min: | max: | min: | max:
Boston(KBOS) | 1 | 10 | 5 | 12
我的目标是这样解压它:
airport_code | localtime | MinTemp | MaxTemp
KBOS | 1/5/2018 | 1 | 10
KBOS | 1/6/2018 | 5 | 12
我的策略是:
if(insertRow == null)
{
insertRow = MyDataSet.tblForecast.NewtblForecastRow();
}
但这会引发“使用未分配的局部变量”错误。关于如何在某些迭代中保留insertRow并在其他迭代中处理它,有什么想法吗?或者,有没有关于更好的方法来完成我想要的任务的建议?代码的相关部分如下所示:
using (TextFieldParser csvParser = new TextFieldParser(FileName))
{
csvParser.SetDelimiters(new string[] { "," });
csvParser.ReadLine(); //Skip top line
string[] dateList = csvParser.ReadFields();//Get dates from second line.
string[] fieldNames = csvParser.ReadFields();//Get headers from third line
//Read through file
while (!csvParser.EndOfData)
{
DataSet1.tblForecastRow insertRow = MyDataSet.tblForecast.NewtblForecastRow();
string[] currRec = csvParser.ReadFields();
//Get airport code
string airportCode = currRec[0].Substring(currRec[0].LastIndexOf("(") + 1, 4);
//Unpivot record
DateTime currDate = DateTime.Parse("1/1/1900");//initialize
DateTime prevDate;
for (int i = 1; i<fieldNames.Length; i++) //skip first col
{
prevDate = currDate;//previous date is the prior current date
DateTime.TryParse(dateList[i], out currDate);//set new current date
int val;
int.TryParse(currRec[i], out val);
switch (fieldNames[i].ToLower())
{
case "min:":
insertRow["MinTemp"] = val;
break;
case "max:":
insertRow["MaxTemp"] = val;
break;
}
if (currDate == prevDate)//if same date, at end of row, insert
{
insertRow["airport_code"] = airportCode;
insertRow["localTime"] = currDate;
insertRow["Forecasted_date"] = DateTime.Today;
MyDataSet.tblForecast.AddtblForecastRow(insertRow);
ForecastTableAdapter.Update(MyDataSet.tblForecast);
}
}
}
}
使用(TextFieldParser csvParser=新的TextFieldParser(文件名))
{
SetDelimiters(新字符串[]{“,”});
csvParser.ReadLine();//跳过顶行
string[]dateList=csvParser.ReadFields();//从第二行获取日期。
string[]fieldNames=csvParser.ReadFields();//从第三行获取标题
//通读文件
而(!csvParser.EndOfData)
{
DataSet1.tblForecastRow insertRow=MyDataSet.tblForecast.NewtblForecastRow();
字符串[]currRec=csvParser.ReadFields();
//获取机场代码
字符串airportCode=currRec[0]。子字符串(currRec[0]。LastIndexOf(“”+1,4);
//非皮沃记录
DateTime currDate=DateTime.Parse(“1/1/1900”);//初始化
日期时间日期;
对于(int i=1;i而言,在处理完当前行后,您将创建一个新行。您已经知道该行的位置:
if (currDate == prevDate)//if same date, at end of row, insert
{
insertRow["airport_code"] = airportCode;
insertRow["localTime"] = currDate;
insertRow["Forecasted_date"] = DateTime.Today;
// we're storing insertRow
MyDataSet.tblForecast.AddtblForecastRow(insertRow);
// now it gets saved (man that is often)
ForecastTableAdapter.Update(MyDataSet.tblForecast);
// OKAY, let's create the new insertRow instance
insertRow = MyDataSet.tblForecast.NewtblForecastRow();
// and now on the next time we end up in this if
// the row we just created will be inserted
}
可以在循环外部创建初始行:
// first row creation
DataSet1.tblForecastRow insertRow = MyDataSet.tblForecast.NewtblForecastRow();
//Read through file
while (!csvParser.EndOfData)
{
// line moved out of the while loop
string[] currRec = csvParser.ReadFields();
添加一个包含五个成员变量airport\u code、localTime、Forecast\u date、MinTemp和MaxTemp的类。填充一个实例,而不是insertRow。在最后一个if中,在添加数据的地方,声明并创建insertRow,从类的实例填充它,并将其添加到datatable。这非常有效,谢谢!是的,我知道我很抱歉更新的方式太频繁:)我只是在测试时有它,这样我就可以看到每一行在SQL中出现。