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:
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 + "`");
}
Takes 4-5 milliseconds to execute (using Stopwatch)
Also you have the packet processing thread do your database submissions for you? If theres latency while submitting to the database your going to be holding back that thread for second after second while waiting for the connection to go through or for the connection to time out. You should really have a seperate thread running processing all of your database submissions so that if there is a problem connecting to the database you dont cause the player experience latency.
I realise that this is for Inserting, however there are many client initiated processes which result in insert queries.
I personally (or well, back when I was doing CO programming) use Subsonic as it's much easier and faster in many ways, but this was if people just wanted an easier way to do their queries, although I do not recommend using normal queries and friends who ask me about help over MSN knows that, I try to lead them to Subsonic, but oh well =p
What pretty much makes this so much easier is the way it's organized, plus that it uses DataReader.GetName(), so you won't need to fill in the column name.
And yes, this is for insert only, but could easily be modified to be able to update, delete etc.
What makes it easy also makes it ineffcient, your making the server do more work than is neccassery just to save yourself the trouble of writing out the command. Also you have to create an array of objects which is not needed, to save yourself the time of writing out the default values once.
If you used stored procedures for example, when you create the procedure on the server you can do this (this is the other half of the procedure i posted earlier):
So you need only send the data which is dynamic, and infact you can create dynamic data durring this procedure at the database side instead of having the server do it.
All in all stored procedures has this beat all over the place >.< and even standard insert sql commands would be faster than what you have released today.
Edit: Just did a stopwatch test on the stored procedure, 1ms.
Nice, perhaps use params object[] Values instead of object[] and also how is the string entering working, didn't work for me without putting extra ' around it.
This way it simplfies to:
PHP Code:
public static bool AddNPC(uint UID, int Type, string Name, int Flags, int Direction, int X, int Y, int Map, int SobType) { Insert("Npcs", UID, Type, 10, Flags, Direction, X, Y, Map, SobType); }
Oh my ***!!! how you found out? that is what i was looking for!
How do you mean with found out? How I got to think of this? Just to make it easier for a friend who was struggeling with 'normal' MySQL queries, I figured this system would make it lots easier ^^
How do you mean with found out? How I got to think of this? Just to make it easier for a friend who was struggeling with 'normal' MySQL queries, I figured this system would make it lots easier ^^
[Release]MySql Databse 5165 04/22/2010 - CO2 PServer Guides & Releases - 7 Replies New Source NewestCoSource Using MySql Database You Can Use it Free !
Sql Tabels :
accounts
characters
config
items
skills
cq_flowers
[HELP]C# MySQL Command insert if not exsist? 09/04/2009 - CO2 Private Server - 8 Replies MySqlCommand Command = new MySqlCommand("INSERT INTO accounts(AccountID,LogonType) VALUES ('" + fillBox.Text + "','3')", Connection);
Now, I want it to check if the value of fillBox.Text doesn't excist before making the account, so It wont create the same account. I tried making it in a try, that didn't help, it just make several accounts with the same name.
~Bas
[RELEASE]MySQL TableEditor 07/25/2009 - CO2 PServer Guides & Releases - 17 Replies Hello e*pvper's!
Here is my first C# tool.
I have made this lite tool for people's who dont know how to use NativCat (or ther program's like this)
For the initial release this tool have the next feature's:
Connect To MySQL Server via IP,User and Password.*
View and Edit table's.
Alter Table's.
Open Close DataBase's (DB's)
[Help CoEmuV2 (PHP/MYSQL required)] Donation system 07/18/2009 - CO2 Private Server - 2 Replies I want an system if someone donated it updates an other column so:
Confirmation Code : put something here // Mysql table: Dragonballs.Dcode
Confirmation Pass : put something here // Mysql table: Dragonballs.Dpass
And then if you click claim it updates the table/column : Characters.Prize
With the amount of : Dragonballs.Damount