Creating a Database with MySQL | Connecting to the database with C#

in #utopian-io7 years ago (edited)

Hello there

I am describing how you can read the data you created using mysql server with C#.

I think this is one of the first projects of C# beginners.

As you know, we use various software to use the database we created with Mysql.The C# We use for desktop software is one of them.

Once you have learned to log in using the database, you can develop this project and read other data from the database.

I will tell you step by step how to create a database with MySQL and how to add the data.

If you wish, you can install the database on a remote server and make a licensed login system.

You will reach the following experiences with this topic.

- Creating a project.
- Adding Button
- Adding Textbox
- Adding Labels
- Form design
- Textbox Passwordchar
- Change the element name.
- Button Click Event Control
- If Else Method
- MessageBox Control
- Program Exit Method
- Creating SQL Database
- Creating Table
- Adding data to a database

if you want to download my test database click here

Project Github Link : https://github.com/oguzdelioglu/C--Form-Login-System


Database Name

db1.png

Enter Your Database Name. Example "teststeemit".

db2.png


Database Name

1.png


Create Table

2.png


Create Column
3.png


Adding data

4.png


DB Connection Codes

private void dbconnect()
{
try
{
conn = "Server=localhost;Database=teststeemit;Uid=root;Pwd=;";
connect = new MySqlConnection(conn);
connect.Open();
}
catch (MySqlException e)
{
throw;
}
}

Add Reference Visual Studio.

5.png


Login Checker Codes

private bool checklogin(string user, string pass)
{
dbconnect();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "Select * from accounts where username=@user and password=@pass";
cmd.Parameters.AddWithValue("@user", user);
cmd.Parameters.AddWithValue("@pass", pass);
cmd.Connection = connect;
MySqlDataReader login = cmd.ExecuteReader();
if (login.Read())
{
    connect.Close();
    return true;
}
else
{
    connect.Close();
    return false;
}
}

Variables

private string conn;
private MySqlConnection connect;

(Button Click Event)

private void button1_Click(object sender, EventArgs e)
{
string user = username.Text;
string pass = password.Text;
if (user == "" || pass == "")
{
MessageBox.Show("Empty Fields Detected ! Please fill up all the fields");
return;
}
bool r = checklogin(user, pass);
if (r)
MessageBox.Show("Correct Login Credentials");
else
MessageBox.Show("Incorrect Login Credentials");
}

Create Project and Design Form

step1.gif


Change Element Names,Using If Else and MessageBox Method with Button Click Event.
step2.gif


Testing Application
step3.gif


Making Close button.Using Application Exit Function with Button Click Event.
step4.gif


Change Textbox value type.I am using now passwordchar "*" character.

step5.gif


step6.gif

I hope it was helpful.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

interesting Good Luck

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @odl I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

This is amazing brother .. Id like to know how to create a programme .. from A to Z ...
Thank you bro keep goin'

It may be well advised to clarify "Windows' Operating System in Use" in your title for those who are allergic to it. ;)

I like it! Subscribe to your blog!