Thursday, 8 August 2013

SQL CE - Memory Leak - Inserting Rows

SQL CE - Memory Leak - Inserting Rows

I have an absolutely massive memory leak using this code. Can you please
tell me where I am going wrong. I am adding close to 50k entries into this
Database (I know it is a bit large for CE) but it shouldn't have to happen
all that often. Any help is appreciated.
public static void AddRow(uint id, string name, uint zone, uint map,
string state,
string type, float x, float y, float z, DataBaseType dbtype)
{
string db = null;
string table = null;
DateTime dateTime = DateTime.Now.Date;
using (var con = new SqlCeConnection(connStr))
{
Logger.WriteDebug(id + ", '" + name + "', '" + zone + "', '" +
map + "', '" + state + "', '" + type +
"', '" + x + "', '" + y + "', '" + z);
/*
string strsql;
strsql = "INSERT INTO " + table + "(Id, Name, Zone, Map,
State, Type, X, Y, Z) VALUES (" + id + ", '" + name + "', '" +
zone + "', '" + map + "', '" + state + "', '" + type + "', '"
+ x + "', '" + y + "', '" + z + "')";
con.Open();
SqlCeCommand cmd1 = new SqlCeCommand(strsql,con);
try
{
cmd1.ExecuteNonQuery();
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
}
* */
//MessageBox.Show(strsql); //that wont work. Not with the
parameters below
using (
var cmd =
new SqlCeCommand(
"INSERT INTO " + table + "(Id, Name, Zone, Map,
State, Type, X, Y, Z, Create_Date, Update_Date) "
+
"VALUES (@Id, @Name, @Zone, @Map, @State, @Type,
@X, @Y, @Z, @Create_Date, @Update_Date)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Zone", zone);
cmd.Parameters.AddWithValue("@Map", map);
cmd.Parameters.AddWithValue("@State", state);
cmd.Parameters.AddWithValue("@Type", type);
cmd.Parameters.AddWithValue("@X", x);
cmd.Parameters.AddWithValue("@Y", y);
cmd.Parameters.AddWithValue("@Z", z);
cmd.Parameters.AddWithValue("@Create_Date", dateTime);
cmd.Parameters.AddWithValue("@Update_Date", dateTime);
con.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
BotControl.StopBot();
}
finally
{
cmd.Dispose();
}
}
con.Close();
con.Dispose();
}
}

No comments:

Post a Comment