Ragazzi, devo leggermi un file di testo e poi una volta letto copiare i dati in una tabella SQL.
I dati dentro il file di testo vengono suddivisi da uno spazio, che separa le colonne.
Questo è il mio codice:
private void CreateDataTableFromFile()
{
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
dc = new DataColumn();
dc.ColumnName = "Date";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Time";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Col1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Col2";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Col3";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Col4";
dt.Columns.Add(dc);
using (StreamReader sr = new StreamReader(@"C:\Documents and Settings\User\Desktop\Text.txt"))
{
string input;
while ((input = sr.ReadLine()) != null)
{
string[] s = input.Split(new char[] { ' ' });
dr = dt.NewRow();
dr["Date"] = s[0];
dr["Time"] = s[1];
dr["Col1"] = s[2];
dr["Col2"] = s[3];
dr["Col3"] = s[4];
dr["Col4"] = s[5];
dt.Rows.Add(dr);
}
sr.Close();
return dt;
}
}
Il problema è che non mi da un errore di indice oltre la matrice...
Sapete come risolvere ?

Rispondi quotando