Support Forums

Full Version: [C#][Tutorial] SQL Database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I originally posted this on HF, http://www.hackforums.net/newreply.php?t...id=3981579

xHtmlPhP's C# SQL Database Tutorial


What we need:
  • 2x Textbox
  • 1x Button

Your page should like like this:


Next step we need to create a database.

Go to Project > Add Windows Form, Now click 'Service Based Database' Then press ok.

Once you've created it a dialog should pop up, Just click finish.

(If your database explorer isn't open, Go to 'View > Other Windows > Database Explorer')

Now we've created a database (you can see it in your database explorer)
Open your database in the explorer then right click the 'Tables' folder.
Click (Add new table)

Now we need to declare our crap in the database.
Create 3 column's
  1. UID
  2. Username
  3. Password

Now for the DataType's.

For the UID the datatype would be 'int' since the userid is going to be a number.

Now for the Username & Password. The datatype will be 'varchar(50)'

Next, As you can see the 'Enable nulls' is checked on all 3, Uncheck them since this isn't wanted.

Now right click as shown in image and click save. Name the table 'user'

Now we can close that database tab and go back to our designer.

Next step we need to make the datasource (Data > Add new Data Source)
This should come up,

Click next then we get another dialog. For this part we need to grab the Connection string.

Copy it as so then click next

We'll now get a new dialog with a list of a few thing's with checkbox's next to them.
  • Tables
  • Views
  • Stored Procedures
  • Functions

Check all of them then click finish.

Now the coding part, double click your login button. Before we do anything we need to add this to our using's
Code:
using System.Data.SqlClient;

Now we can access the correct tool's.

On your button code declare these
Code:
SqlConnection connection = new SqlConnection();
            SqlCommand command = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet data = new DataSet();

Now, remember the piece of code you copied earlier? This is where we use it
Code:
connection.ConnectionString = (@"");
Inbetween the quote's add the code you copied earlier.

Next we need to read the database.
Code:
command.CommandText = "SELECT * FROM [User] WHERE Username='" + textBox1.Text + "'AND Password='" + textBox2.Text + "';";
It will access our 'User' table.

Next, add this
Code:
connection.Open();

            command.Connection = connection;

            adapter.SelectCommand = command;
            adapter.Fill(data, "0");

            int count = data.Tables[0].Rows.Count;

Now, we'll add user's to the database! Go back to your database explorer, Now right click your user table and click 'Show table data'

Add a few username's/password's with the UID increasing for each user.


Now we've added the user's!

Next we need to go back to the code and check if the user exist's. Adding an if statement will let us execute a command for if the login is correct and if it's incorrect.

The code is:
Code:
if (count > 0)
            {
                MessageBox.Show("Welcome, " + textBox1.Text + ". Your name exist's on our database!");
            }
            else
            {
                MessageBox.Show("Your name does not exist on the database");
                textBox1.Clear();
                textBox2.Clear();
            }

Now we have a working login system!

Sorry about the tutorial being messy. Kept forgetting thing's and just adding them in random place's, Anyway I hope you learnt a bit from this tutorial and find it helpful

Victoire
VEry nice tut Thanks For sharing
Very informative TUT

Thanks!
Nice quality of information,
Thanks for sharing.