면책 조항에 정수와 같은 문자열을 주조 : 이것은 내가 MSSQL에 CSV를 가져올 시도하지만 위생을 위해 먼저 CSV 값을 반복 할 필요가 있어요 내 첫 닷넷 C# 프로젝트는 .NET DataTable을
입니다. CSV의 일부 열은 정수가되며 (나중에 계산에 사용됩니다) 일부는 일반 varchar입니다.
위의 스크립트는 SQL이 정수로 문자열을 쓸 수 없을 때 내 응용 프로그램에서 나중에 예외를 throw하는 문자열로 DataTable의 모든 값 (행 열 값)을 강제로 나타냅니다.
여기 내 데이터 테이터를 생성하고 채우는 getCSVImport에 사용하는 방법입니다.
내가 생각하는 것은 값이 정수인지 확인한 다음 정수로 캐스팅하는 다른 조건을 추가하는 것입니다. PHP는 유형을 그렇게 강력하게 처리하지 않으므로이 유형이 새로운 것입니다.하지만 두려움이 있습니다. dataTable 내에서 값을 여러 유형으로 혼합 할 수 있는지 확실하지 않으므로 작동하지 않습니다.
제 질문은 다른 유형의 데이터 테이블에 다른 값을 갖는 방법이 있습니까? 내 코드는 전체 줄을 받아서 문자열로 쓰고 문자열이나 정수로 값을 지정해야합니다.
/*
* getCsvData()
* This method will create a datatable from the CSV file. We'll take the CSV file as is.
* and collect the data as needed:
*
* - Remove those original 4 lines (worthless info)
* - Line 5 starts with the headers, remove any of the brackets around the values
* - Iterate through the rest of the fields and sanitize them before we add it to the datatable
*
*/
private DataTable getCsvData(string csv_file_path)
{
// Create a new csvData tabletable object:
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
int row = 1;
while (!csvReader.EndOfData)
{
// Read the string and collect the row data
string[] rowData = csvReader.ReadFields();
if (row <= 4)
{
// We want to start on row 5 as first rows are nonsense :)
// Incriment the row so that we can do our magic above
row++;
continue;
} if(row == 5)
{
// Row 5 is the headers, we need to sanitize and continue:
foreach (string column in rowData)
{
// Remove the [ ] from the values:
var col = column.Substring(1, column.Length - 2);
DataColumn datecolumn = new DataColumn(col);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
// Incriment the row so that we can do our magic above
row++;
} else
{
// These are all of the actual rows, sanitize and add the rows:
//Making empty value as null
for (int i = 0; i < rowData.Length; i++)
{
// First remove the brackets:
if (rowData[i].Substring(0,1) == "[")
{
rowData[i] = rowData[i].Substring(1, rowData[i].Length - 2);
}
// Set blank to null:
if (rowData[i] == "" || rowData[i] == "-")
{
rowData[i] = null;
}
// Lastly, we need to do some calculations:
}
// Add the sanitized row to the DataTable:
csvData.Rows.Add(rowData);
}
}
}
}
catch (Exception ex)
{
throw new Exception("Could not parse the CSV file: "+ ex.Message);
}
return csvData;
}
정확히 무엇입니까? – Kinetic
문자열을 정수로 "캐스팅"할 수는 없지만이를 구문 분석 할 수는 있습니다. 'int.Parse' 또는'int.TryParse'를 살펴보십시오. – Kroltan
csv 파일의 열이 숫자이고 텍스트가 미리 알 수 있습니까? 또는 실행 사이에 변경 될 수 있습니까? –