매우 간단한 메서드를 사용하여 csv를 SQL 데이터베이스에 삽입 할 수 있도록 데이터 테이블로 변환합니다. 빈 문자열을 DBNull.Value로 바꾸지 않으면 정확히 어떻게 작동해야합니까? 그렇다면 ArrayTypeMismatchException이 발생합니다. 들어오는 스트림은 데이터가있는 단순한 쉼표로 구분 된 파일입니다.배열 객체를 가진 ArrayTypeMismatchException 얻기
문제 코드 :
public static DataTable StreamToTable(Stream stream, bool headersAvailable, bool convertBlankToDBNull)
{
DataTable data = new DataTable();
StreamReader reader = new StreamReader(stream);
if (!reader.EndOfStream)
{
if (headersAvailable)
{
try
{
//get headers from first line
string[] headers = reader.ReadLine().Split(',');
//construct headers
for (int i = 0; i < headers.Length; i++)
{
data.Columns.Add(headers[i]);
}
}
catch (IOException ioEx)
{
return null;
}
}
while (!reader.EndOfStream)
{
object[] row = reader.ReadLine().Split(',');
if (convertBlankToDBNull)
{
for (int i = 0; i < row.Length; i++)
{
if (row[i].ToString().Equals(""))
{
row[i] = DBNull.Value; //this is where I get the exception
}
}
}
data.Rows.Add(row);
}
return data;
}
else return null;
}
나는 객체 배열이기 때문에 배열에 무엇을 할당 할 수 있어야합니다 ... 내가 잘못하고있는 중이 야 알아낼 수 없기 때문에이 방법이 될 수 있습니다 형식 불일치?
감사합니다 순전히! 당신은 저에게 앞으로도 도움이 될만한 것을 보여주었습니다. – jbenscoter
나는 비슷한 문제에 관해서 벽에 맞설 준비가되었다. 이 대답은 대단히 도움이되었습니다! 도와 줘서 고마워!! – christopheralan88