打开 OracleConnection 时出现 OutOfMemoryException
OutOfMemoryException while while opening OracleConnection
我和一位同事的任务是解决 SQL-Client 上的问题。
错误是 System.OutOfMemoryException.
我想这可能是因为客户端有很多连接没有正确关闭。
我测试了一下,但似乎无法正常工作。
有人有什么建议吗?
这是发生错误的代码:
public void SQLIntoDataGridView(OracleCommand cmd, int startIndex, int maxIndex, int ersteSpalte, int maxSpalten, DataTable datenTabelle, DataGridView dgv, int sqlNr, DataTable dt)
{
OracleConnection test = new OracleConnection();
int i = 0;
int j = 0;
int spalten = ersteSpalte;
int id;
int spaltenZaehler = ersteSpalte;
int maxrows = datenTabelle.Rows.Count;
int readZeile = 0;
int writeZeile = 0;
int startrow = 0;
int startSpalte = 0;
int endSpalte = 0;
Stopwatch watch = new Stopwatch();
watch.Start();
test.ConnectionString = "Data Source=; User ID=; Password=";
using (test)
{
if (test.State == ConnectionState.Closed)
test.Open(); // <-- this is where the error occures
using (OracleDataReader reader = cmd.ExecuteReader())
{
datenTabelle.Load(reader);
dataGridViewErgebnis.DataSource = dt;
dataGridViewTest.DataSource = datenTabelle;
dataGridViewErweitern(sqlNr);
#region Rechnungsempfänger X
if (startIndex == 27 || startIndex == 24)
{
while (j < dataGridViewErgebnis.Rows.Count - 1)
{
id = startIndex;
if (dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString() == "")
{
readZeile++;
i = readZeile;
j++;
writeZeile++;
}
else if (dataGridView1.Rows[startrow].Cells[0].Value.ToString() == dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString())
{
if (startIndex == 24)
{
startSpalte = 7;
endSpalte = 9;
spaltenZaehler = startSpalte;
}
else if (startIndex == 27)
{
startSpalte = 10;
endSpalte = 14;
spaltenZaehler = startSpalte;
}
spaltenZaehler = startSpalte;
maxSpalten = endSpalte;
while (spaltenZaehler <= maxSpalten)
{
i = startrow;
dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dgv.Rows[i].Cells[spaltenZaehler].Value.ToString();
spaltenZaehler++;
id++;
}
readZeile++;
i = readZeile;
j++;
writeZeile++;
spaltenZaehler = startSpalte;
startrow = 0;
}
else
{
startrow++;
}
if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
{
id = startIndex;
while (id <= maxIndex && spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dataGridViewTest.Rows[i].Cells[spaltenZaehler].Value.ToString();
id++;
spaltenZaehler++;
}
}
}
}
#endregion
else
{
while (i < dt.Rows.Count - 1)
{
id = startIndex;
if (dataGridViewErgebnis.Rows[i].Cells[0].Value.ToString() == dataGridViewTest.Rows[j].Cells[0].Value.ToString())
{
while (spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
spaltenZaehler++;
id++;
}
if (startIndex == 10 && j == datenTabelle.Rows.Count - 1)
{
j--;
j--;
i++;
}
if (j <= datenTabelle.Rows.Count - 1)
{
j++;
}
spaltenZaehler = ersteSpalte;
}
else
{
i++;
while (id <= maxIndex)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = "";
id++;
}
}
if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
{
id = startIndex;
while (id <= maxIndex && spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
id++;
spaltenZaehler++;
}
}
else if (j == datenTabelle.Rows.Count && startIndex != 10)
{
j--;
if (i <= dt.Rows.Count - 2)
{
i++;
}
}
}
}
watch.Stop();
StoppUhr(watch);
}
}
}
我会像这样重写代码。
.
.
.
OracleConnection test = new OracleConnection();
.
.
.
using (OracleConnection test = new OracleConnection("Data Source=; User ID=; Password=")) // <-- SQLconnection here
{
test.Open(); // <-- Only Open connection
using (OracleDataReader reader = cmd.ExecuteReader())
{
.
.
.
}
}
我和一位同事的任务是解决 SQL-Client 上的问题。 错误是 System.OutOfMemoryException.
我想这可能是因为客户端有很多连接没有正确关闭。
我测试了一下,但似乎无法正常工作。
有人有什么建议吗?
这是发生错误的代码:
public void SQLIntoDataGridView(OracleCommand cmd, int startIndex, int maxIndex, int ersteSpalte, int maxSpalten, DataTable datenTabelle, DataGridView dgv, int sqlNr, DataTable dt)
{
OracleConnection test = new OracleConnection();
int i = 0;
int j = 0;
int spalten = ersteSpalte;
int id;
int spaltenZaehler = ersteSpalte;
int maxrows = datenTabelle.Rows.Count;
int readZeile = 0;
int writeZeile = 0;
int startrow = 0;
int startSpalte = 0;
int endSpalte = 0;
Stopwatch watch = new Stopwatch();
watch.Start();
test.ConnectionString = "Data Source=; User ID=; Password=";
using (test)
{
if (test.State == ConnectionState.Closed)
test.Open(); // <-- this is where the error occures
using (OracleDataReader reader = cmd.ExecuteReader())
{
datenTabelle.Load(reader);
dataGridViewErgebnis.DataSource = dt;
dataGridViewTest.DataSource = datenTabelle;
dataGridViewErweitern(sqlNr);
#region Rechnungsempfänger X
if (startIndex == 27 || startIndex == 24)
{
while (j < dataGridViewErgebnis.Rows.Count - 1)
{
id = startIndex;
if (dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString() == "")
{
readZeile++;
i = readZeile;
j++;
writeZeile++;
}
else if (dataGridView1.Rows[startrow].Cells[0].Value.ToString() == dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString())
{
if (startIndex == 24)
{
startSpalte = 7;
endSpalte = 9;
spaltenZaehler = startSpalte;
}
else if (startIndex == 27)
{
startSpalte = 10;
endSpalte = 14;
spaltenZaehler = startSpalte;
}
spaltenZaehler = startSpalte;
maxSpalten = endSpalte;
while (spaltenZaehler <= maxSpalten)
{
i = startrow;
dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dgv.Rows[i].Cells[spaltenZaehler].Value.ToString();
spaltenZaehler++;
id++;
}
readZeile++;
i = readZeile;
j++;
writeZeile++;
spaltenZaehler = startSpalte;
startrow = 0;
}
else
{
startrow++;
}
if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
{
id = startIndex;
while (id <= maxIndex && spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dataGridViewTest.Rows[i].Cells[spaltenZaehler].Value.ToString();
id++;
spaltenZaehler++;
}
}
}
}
#endregion
else
{
while (i < dt.Rows.Count - 1)
{
id = startIndex;
if (dataGridViewErgebnis.Rows[i].Cells[0].Value.ToString() == dataGridViewTest.Rows[j].Cells[0].Value.ToString())
{
while (spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
spaltenZaehler++;
id++;
}
if (startIndex == 10 && j == datenTabelle.Rows.Count - 1)
{
j--;
j--;
i++;
}
if (j <= datenTabelle.Rows.Count - 1)
{
j++;
}
spaltenZaehler = ersteSpalte;
}
else
{
i++;
while (id <= maxIndex)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = "";
id++;
}
}
if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
{
id = startIndex;
while (id <= maxIndex && spaltenZaehler <= maxSpalten)
{
dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
id++;
spaltenZaehler++;
}
}
else if (j == datenTabelle.Rows.Count && startIndex != 10)
{
j--;
if (i <= dt.Rows.Count - 2)
{
i++;
}
}
}
}
watch.Stop();
StoppUhr(watch);
}
}
}
我会像这样重写代码。
.
.
.
OracleConnection test = new OracleConnection();
.
.
.
using (OracleConnection test = new OracleConnection("Data Source=; User ID=; Password=")) // <-- SQLconnection here
{
test.Open(); // <-- Only Open connection
using (OracleDataReader reader = cmd.ExecuteReader())
{
.
.
.
}
}