Post: C# Mysql connection tutorial
11-05-2013, 01:01 AM #1
SNMT| Havoc
Do a barrel roll!
(adsbygoogle = window.adsbygoogle || []).push({}); Hey guys SNMT|HACK here and i wanted to show you what i learned

I learned to make a sql connection to a site and a login

Reason im showing this is because mysql is easily the easiest connection to hijack users info.

I WILL NOT TELL ANY WAY OF HACKING THE INFO

You'll need the binary you get if you go under add a resource then .net then find a mysql. You also need the connector which you can get from this site : You must login or register to view this content.

    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using ProjectWater; //Spoiler of my new rtm tool =D

namespace test_program
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string MyConString = @"server=SERVER IP OR NAME;userid=USERNAME ON SERVER;password=Password of user;database=The database";

MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select * from users";


try
{
connection.Open();
}
catch (Exception ex)
{
MessageBox.Show("There has been an error connecting to the user database! Please try again later.");
}
command.CommandText = "SELECT * FROM users WHERE uname = @username AND password = @password";

//Retrieve row
command.Parameters.AddWithValue("@username", textBox1.Text);
command.Parameters.AddWithValue("@password", textBox2.Text);
Reader = command.ExecuteReader();
try
{
Reader.Read();
string uname = (string)Reader.GetValue(3);
string pword = (string)Reader.GetValue(4);
}
catch (Exception ex)
{
MessageBox.Show("Username or Password incorrect");
return;
}
UInt32 uid = (UInt32)Reader.GetValue(0);
string fname = (string)Reader.GetValue(1);
string lname = (string)Reader.GetValue(2);
string email = (string)Reader.GetValue(5);
string type = (string)Reader.GetValue(6);
string status = (string)Reader.GetValue(7);
string isregistered = (string)Reader.GetValue(Cool Man (aka Tustin);
if (status == "active" && type == "admin" || type == "elite" || type == "staff")
{
MessageBox.Show("Logged in ", "Mw3 mod tool", MessageBoxButtons.OK, MessageBoxIcon.Information);
Form2 fm2 = new Form2();
fm2.Show();
}
else
{
if (status == "banned" || status == "deleted")
{
MessageBox.Show("Unable to log in reason: Account Is Banned");
}

if (type == "normal")
{
MessageBox.Show("Unable to log in reason: Account is not elite");
}
}
}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("SITE IS PRIVATE FOR NOW Happy");
}

}
}



My table is called users and there are these rows


1. userid (1-unlimited)
2. first name
3. last name
4. user name
5. password
6. email
7. type (admin, elite, staff, normal)
8. status (deleted, banned, active)


- SNMT| HACK
(adsbygoogle = window.adsbygoogle || []).push({});
11-05-2013, 08:45 AM #2
Nice that you used try and catch statements. Not many programmers know that it's implemented.

The following user thanked TheUnexpected for this useful post:

SNMT| Havoc
11-05-2013, 05:10 PM #3
Default Avatar
Oneup
Guest
You forgot to mention that in order to even do anything with mysql you need to download their binaries and import them.
You must login or register to view this content.
11-05-2013, 09:27 PM #4
QuantumDev
Can’t trickshot me!
Nice thread Happy Whenever using some type of code on your thread, try to wrap your code with the
     tags.
11-06-2013, 04:32 PM #5
Complete Speed
Do a barrel roll!
Originally posted by TheUnexpected View Post
Nice that you used try and catch statements. Not many programmers know that it's implemented.


is there any other way to catch exceptions?
11-06-2013, 04:49 PM #6
Default Avatar
Oneup
Guest
Originally posted by Complete
is there any other way to catch exceptions?


You could manually do it by writing an if statement for any possible error. But there's no reason to with a try catch.

However once you get into OOP being able to throw those exceptions is nice
11-06-2013, 09:15 PM #7
spudeeelad
I defeated!
Originally posted by TheUnexpected View Post
Nice that you used try and catch statements. Not many programmers know that it's implemented.

Should probably add using statements;
    
using (MySqlConnection connection = new MySqlConnection(MyConString))
{
try
{
using (MySqlCommand command = new MySqlCommand(command, connection))
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
}
}

With these using statements, there is no need to add have connection.Open() or .Close(), the using does that for you.

Also, if you want to do it that way, you should also have
    
connection.Dispose();


Finally, within the using statements, there is no need to use the reader in that way, far easier to do this;
    
using (MySqlCommand command = new MySqlCommand(command, connection))
{
DataTable dt = new DataTable();

reader = command.ExecuteReader();

dt.Load(reader);

}

then just retrieve the values from the DataTable.
11-09-2013, 12:46 AM #8
SNMT| Havoc
Do a barrel roll!
Originally posted by Complete
is there any other way to catch exceptions?




Im not sure, i only know try and catch im new to programming Happy
11-09-2013, 12:49 AM #9
SNMT| Havoc
Do a barrel roll!
Thanks for reminding me !
11-10-2013, 05:28 AM #10
Wow, very nice tutorial. Great work!

The following user thanked Heistful for this useful post:

SNMT| Havoc

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo