Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C#][Tutorial] SQL Database
#1
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
Reply
#2
VEry nice tut Thanks For sharing
[Image: OGForumsbanner.png]
Reply
#3
Very informative TUT

Thanks!
Reply
#4
Nice quality of information,
Thanks for sharing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,529 08-30-2012, 10:14 PM
Last Post: Resistance
  [Source]Sql Database Project [Source] Digital-Punk 0 1,439 10-16-2011, 07:01 AM
Last Post: Digital-Punk
  {TuToRiAl} How To Verify If Your App. Was Ran As Administrator {TuToRiAl} Resistance 11 6,014 10-11-2011, 04:30 PM
Last Post: Genuine
  {TuToRiAl} How To Create .GIF Animations for Your VB Tutorials {TuToRiAl} Resistance 12 3,840 05-11-2011, 06:53 AM
Last Post: cOld fus1on
  [TuToRiAl] Helpful Newbie Visual Basic Code Snippets[/TuToRiAl] Resistance 5 2,126 05-08-2011, 02:26 PM
Last Post: PURP

Forum Jump:


Users browsing this thread: 1 Guest(s)