Nick
2007-01-08 15:51:29 UTC
Ciao a tutti,
ho realizzato una piccola procedura per recuperare i dati da un file di
testo e inserirli in un database access; tecnicamente funziona tutto,
solo che se faccio l'importazione dell'intero archivio, dopo circa 2000
righe il programma si pianta emettendo il messaggio "Risorse di sistema
insufficienti". Il codice che ho usato è il seguente:
private void button1_Click(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("e:\\file.txt");
string sLine = "";
string str1, str2, str3, str4, str5, str6, str7, str8;
string stringaSql;
while (sLine != null)
{
sLine = objReader.ReadLine();
campo1 = sLine.Substring(1, 4).Replace(" ", "0");
campo2 = sLine.Substring(8, 39).Trim().Replace("'", "''");
sLine = objReader.ReadLine();
campo3 = sLine.Substring(8, 32).Trim().Replace("'", "''");
campo4 = sLine.Substring(41, 5).Trim();
campo5 = sLine.Substring(48, 22).Trim().Replace("'", "''");
campo6 = sLine.Substring(71, 2).Trim();
campo7 = sLine.Substring(83, 18).Trim();
campo8 = sLine.Substring(101, 18).Trim();
stringaSql = "INSERT INTO tabella (campo1, campo2, campo3,
campo4, campo5, campo6, campo7, campo8) " +
"VALUES('" + str1 + "', '" + str2 + "', '" + str3 + "', '"
+ str4 + "', '" + str5 + "', '" + str6 + "', '" + str7 + "', '" + str8
+ "')";
EseguiQuery(stringaSql);
sLine = objReader.ReadLine();
}
objReader.Close();
MessageBox.Show("Fatto");
}
private void EseguiQuery(string stringaSql)
{
try
{
dataConnection.ConnectionString = connectionString;
dataConnection.Open();
OleDbCommand dataCommand = new OleDbCommand();
dataCommand.Connection = dataConnection;
dataCommand.CommandText = stringaSql;
dataCommand.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show("Errore nell'accesso al database: " +
e.Message);
MessageBox.Show(stringaSql);
}
finally
{
dataConnection.Close();
}
}
Qualcuno sa dirmi dove sbaglio?
Nick
ho realizzato una piccola procedura per recuperare i dati da un file di
testo e inserirli in un database access; tecnicamente funziona tutto,
solo che se faccio l'importazione dell'intero archivio, dopo circa 2000
righe il programma si pianta emettendo il messaggio "Risorse di sistema
insufficienti". Il codice che ho usato è il seguente:
private void button1_Click(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("e:\\file.txt");
string sLine = "";
string str1, str2, str3, str4, str5, str6, str7, str8;
string stringaSql;
while (sLine != null)
{
sLine = objReader.ReadLine();
campo1 = sLine.Substring(1, 4).Replace(" ", "0");
campo2 = sLine.Substring(8, 39).Trim().Replace("'", "''");
sLine = objReader.ReadLine();
campo3 = sLine.Substring(8, 32).Trim().Replace("'", "''");
campo4 = sLine.Substring(41, 5).Trim();
campo5 = sLine.Substring(48, 22).Trim().Replace("'", "''");
campo6 = sLine.Substring(71, 2).Trim();
campo7 = sLine.Substring(83, 18).Trim();
campo8 = sLine.Substring(101, 18).Trim();
stringaSql = "INSERT INTO tabella (campo1, campo2, campo3,
campo4, campo5, campo6, campo7, campo8) " +
"VALUES('" + str1 + "', '" + str2 + "', '" + str3 + "', '"
+ str4 + "', '" + str5 + "', '" + str6 + "', '" + str7 + "', '" + str8
+ "')";
EseguiQuery(stringaSql);
sLine = objReader.ReadLine();
}
objReader.Close();
MessageBox.Show("Fatto");
}
private void EseguiQuery(string stringaSql)
{
try
{
dataConnection.ConnectionString = connectionString;
dataConnection.Open();
OleDbCommand dataCommand = new OleDbCommand();
dataCommand.Connection = dataConnection;
dataCommand.CommandText = stringaSql;
dataCommand.ExecuteNonQuery();
}
catch (Exception e)
{
MessageBox.Show("Errore nell'accesso al database: " +
e.Message);
MessageBox.Show(stringaSql);
}
finally
{
dataConnection.Close();
}
}
Qualcuno sa dirmi dove sbaglio?
Nick