I am going to use MySQL as an example, since I don't have SQL installed. However, if you're set on using SQL, just remove
My
from
MySQL
and It should work for SQL providing you use a connection string built for SQL server with your own credentials. This example code was pulled from a static class in a working MySQL project. For your sanity, I will remove the static modifiers and the classes, and assume you know how to structure the code yourself in a custom database class.
Your connection string would look
Similar to this ::
public const string cString = "server=localhost;user id=root;password=root;persistsecurityinfo=True;database=visuals_db;port=8889";
I think the connection string is self explanatory, so I won't bother elaborating on it; suffice to say you can compare them at ConnectionStrings.Com -
MySQL -
SQLServer - Notice the difference with SQL, you need to qualify an instance name for your SQL installation as where with MySQL, you can provide a server address.
In order to begin executing statements, you will need some which look like this ::
public const string execSelectUnameQuery = "SELECT username from users WHERE email=@email";
public const string execSelectIDQuery = "SELECT id from users WHERE email=@email";
public const string execUpdateNameQuery = "UPDATE users SET name=@Fname WHERE email=@Email AND password=@Pass;";
public const string execInsertUserQuery = "INSERT INTO users (name,username,email,password) VALUES (@Fname,@Uname,@Email,@Pass)";
public const string execDeleteUserQuery = "DELETE FROM users WHERE id=@id";
Next step it to declare a connection with the connection string ::
public MySqlConnection Con = new MySqlConnection(cString);
Then you will want to build up a method to execute your statements. I prefer to pass in my statements to the method. At this point, you're dealing with pseudo code, as I explained, I've pulled this from a working project. It's up to you to jigsaw it back together.
public bool Insert(string Statement)
{
try
{
if (Con != null)
{
DoConnection();
using (MySqlCommand cmd = new MySqlCommand(Statement, Con))
{
cmd.Parameters.AddWithValue("@Fname", myAccount.Name);
cmd.Parameters.AddWithValue("@Uname", myAccount.Username);
cmd.Parameters.AddWithValue("@Email", myAccount.Email);
cmd.Parameters.AddWithValue("@Pass", myAccount.Password);
cmd.ExecuteNonQuery();
}
DoConnection();
return true;
}
return false;
}
catch (Exception ex)
{
MessageBox.Show(string.Concat("Tell the developer there is an error at ", ex.StackTrace));
return false;
}
I prefer to handle my open connections with a switch.
DoConnection()
method being called above opens and closes the connection based on its state.
public bool DoConnection()
{
switch (Con.State == ConnectionState.Closed)
{
case true:
Con.Open();
return true;
case false:
Con.Close();
return false;
}
return false;
}
You will need to get it working yourself. Your database table should look something like this ::
If you want to use the Select/Delete queries, you need to add an ID field to the table and make it AI. Normally I don't provide code snipped off the bat without seeing what you've attempted first. But in a nutshell, It should look something like that. If you have any questions, be sure to post back with your own implementation of how you implemented this example. Hope it helps.