[C#][Tutorial] SQL Database - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19) +---- Thread: [C#][Tutorial] SQL Database (/showthread.php?tid=7011) |
[C#][Tutorial] SQL Database - xHtmlPhP - 05-13-2010 I originally posted this on HF, http://www.hackforums.net/newreply.php?tid=415082&pid=3981579 xHtmlPhP's C# SQL Database Tutorial What we need:
Your page should like like this: Spoiler (Click to View) Next step we need to create a database. Go to Project > Add Windows Form, Now click 'Service Based Database' Then press ok. Spoiler (Click to View) 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
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' Spoiler (Click to View) 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, Spoiler (Click to View) Click next then we get another dialog. For this part we need to grab the Connection string. Copy it as so then click next Spoiler (Click to View) We'll now get a new dialog with a list of a few thing's with checkbox's next to them.
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(); Now, remember the piece of code you copied earlier? This is where we use it Code: connection.ConnectionString = (@""); Next we need to read the database. Code: command.CommandText = "SELECT * FROM [User] WHERE Username='" + textBox1.Text + "'AND Password='" + textBox2.Text + "';"; Next, add this Code: connection.Open(); 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. Spoiler (Click to View) 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) 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 RE: [C#][Tutorial] SQL Database - SouR'D - 05-14-2010 VEry nice tut Thanks For sharing RE: [C#][Tutorial] SQL Database - UniversalForumz - 05-14-2010 Very informative TUT Thanks! RE: [C#][Tutorial] SQL Database - //Pretextâ„¢ - 05-18-2010 Nice quality of information, Thanks for sharing. |