Ello!
First of all, this is NOT sourcedependent, which means you can use this on any source without modifying the code.
This is an replacement for all of you who still uses normal MySQL queries. Use this code to clean up, make it easier and more orginazed.
Here's how it looks:
Example: (Original (LOTF for example))
Example: (My way)
So basically, you create an object array, and send it to the Insert void. Also, the code finds the columnnames by itself, so you don't need to define them. Here's the insert code:
Have fun!
First of all, this is NOT sourcedependent, which means you can use this on any source without modifying the code.
This is an replacement for all of you who still uses normal MySQL queries. Use this code to clean up, make it easier and more orginazed.
Here's how it looks:
Example: (Original (LOTF for example))
Quote:
public static void AddNPC(int UID, int Type, string Name, int Flags, int Direction, int X, int Y, int Map, int SobType)
{
MySqlCommand Command = new MySqlCommand("INSERT INTO npcs (UID,Type,Name,Flags,Direction,X,Y,Map,SobType) VALUES ("
+ UID + ",'" + Type + ",'" + Name + ",'" + Flags + ",'" + Direction + ",'" + X + ",'" + Y + ",'" + Map + ",'" + SobType + ")", Connection);
}
Example: (My way)
Quote:
public static void AddNPC(int UID, int Type, string Name, int Flags, int Direction, int X, int Y, int Map, int SobType)
{
object[] npc = new object[9];
npc[0] = UID;
npc[1] = Type;
npc[2] = 10;
npc[3] = Flags;
npc[4] = Direction;
npc[5] = X;
npc[6] = Y;
npc[7] = Map;
npc[8] = SobType;
Insert("npcs", npc);
}
So basically, you create an object array, and send it to the Insert void. Also, the code finds the columnnames by itself, so you don't need to define them. Here's the insert code:
Takes 4-5 milliseconds to execute (using Stopwatch)Quote:
static void Insert(string table, object[] objects)
{
MySqlDataReader rdr = new MySqlCommand("SELECT * FROM `" + table + "`",connection).ExecuteReader();
if (objects.Length != rdr.FieldCount)
{
if (objects.Length > rdr.FieldCount)
Console.WriteLine("INSERT:FAIL : Too many values");
else
Console.WriteLine("INSERT:FAIL : Need more values");
}
string query = "INSERT INTO " + table + " (";
if(rdr.Read())
{
for (int i = 0; i < rdr.FieldCount; i++)
{
query += rdr.GetName(i);
if (i != rdr.FieldCount - 1)
query += ",";
else
query += ")";
}
query += " VALUES (";
for (int x = 0; x < objects.Length; x++)
{
query += objects[x];
if (x != objects.Length - 1)
query += ",";
else
query += ")";
}
}
rdr.Close();
new MySqlCommand(query,connection).ExecuteNonQuery();
Console.WriteLine("Successfully added a row to `" + table + "`");
}
Have fun!