Using LINQ... erm... but ok..
You should add on how to do it using DataSets and executing Queries.
Needed: Microsoft SQL server 2008, Vistual studio 08/VC# 08 +
Create a new project in VS/VC#.
Project name: "TutorialCsToSql - Tutorial1"
What you'll need:
1linkLabel
1groupBox
2labels
1textbox
1maskedTextBox
3buttons
This is the setup:
rename all of it to the below given names:
lLbLogin
gBLogin
Label1
Label2
tBAcc
mTBPass
bLogin
bReg
bCancelLog
, when your done with this you can select the groupbox and tick Visible to False in the propertybox, then select the bReg got to the propertybox and select Enabled False.
if you press F5, you can see that only the LinkLabel is left.
now on toward SQL server 08, openup server management studio. mine already had a autocreated Database to connect to if it doesnt for you find a tut that does the trick :>.
but lets continue... select the folder Databases and right mouseclick on it... then select New Database.
Name it AccountLoginNRights press OK. now tick the ******* of the Databases a folder named AccountLoginNRights is added, tick this folder too. As you can see there are a couple of folders Select the Table folder rightclick and New Table...
first Rename the Table by going to the properties menu (if not available either press F4 or go to View > Properties Window , in the topbar.) goto (Name) rename it to ACCOUNTSTABLE. with the correct naming relocate to the center of the screen and look a the Column Name underneat it you can enter subTables for this project we'll be using these:
now select AccountId, underneath there is a box named Column Properties, go to Identity Specification and tick the *******. Now select (Is Identity) and set it to "Yes" as you can see identity Increment = 1 this means that when ever a id gets created this will automaticly be counting up if there will be another one.Code:Column Name | Data Type | Allow Nulls AccountId | int | [ ] Account | text | [ ] Password | text | [ ] Value | text | [v]
now select Value since this is Nullable it will obtain a value automaticly or aint needed. go to Column Properties again and now go to Default Value or Binding, typ the following '2' this will be of use later when we will go to a registery and Adminship application.
finally we are going to set a primary key to the AccountId so its un editable. also the top of the table, rightclick AccountId and select Set Primary Key.
to me all seems alright to go forth if it looks like this:
if it does rightclick on the tab or got to File and select "Save ACCOUNTSTABLE"
when you dropdown the Table box you'll notice that the table dbo.ACCOUNTSTABLE has been added. now to creat the basic commands right click it and select "Edit Top 200 Rows".
This is what you'll get
what you need to now is this...
leave the first box open this one will be added automaticly when you press enter. if it doesnt just rightclick on the whitespace below and select " ! Excecute SQL "Code:AccountId | Account | Password | Value | admin | admin | 1
now create a test account too just do the same as above but this time lets keep Value Clear:
when you press enter youll notice there is a ( ! ) infront of the line... now use excecute SQL to solve it. Value auto switched to 2 since we stated it would be 2 on Null.Code:AccountId | Account | Password | Value 1 | admin | admin | 1 | test | test |
we are making progress...
we have a running Database and project.
now its time to link them together.
you can save the SQL Database for now and close it, go back to the VS/VC# project.
goto the Solution explorer. right mousebutton on TutorialCsToSql - Tutorial1 and select Add > New Item..., Select "LINQ to SQL Classes" and name it "AccountDatabase" then press OK. what you get now is a menu for adding tables. click on Server Explorer.
@ the Server Explorer you have a folder named DataConnections... rightclick it and add connection, there is a ******* on Server Name normally its in there if not change the data recourses. select the *\SQLEXPRESS , then go to Connect to a database , Select "AccountLoginNRights" from the drop down box and press OK. Now Tick *\sqlexpress/AccountLoginNRights.dbo, Tables, and click 'n drag ACCOUNTSTABLE to the Center of the screen.
you should get this screen
now select AccountId and go to the Properties select Read Only and set it to True to block a conflict that may occur when you want to add stuff to the server.
Finally we can start the coding of the project.
Open all items in Design so these will be set in the Code list.
First of ill post all code then explain each partyou cant simply copy paste all the names arent correct (im not really into hardcoding)
[php]
namespace TutorialCsToSql
{
public partial class Form1 : Form
{
public int LogCount = 0;
public int LoginAttemptBlock = 0;
public double LoginBlockedReleaser = 0;
public DateTime DateCheckBlock = new DateTime();
AccountDatabaseDataContext ACCDATA = new AccountDatabaseDataContext();
public Form1()
{
InitializeComponent();
}
public void StatusOffline()
{
gBLogin.Visible = false;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (LogCount == 0)
{
gBLogin.Visible = true;
}
else if (LogCount == 1)
{
lLbLogin.Text = "Login";
LogCount = 0;
StatusOffline();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
private void bLogin_Click(object sender, EventArgs e)
{
List<ACCOUNTSTABLE> AccountLogs = (from Acc in ACCDATA.ACCOUNTSTABLEs orderby Acc.AccountId select Acc).ToList();
for (int i = 0; i <= AccountLogs.Count; i++)
{
try
{
if (tBAcc.Text == AccountLogs[i].Account && mTBPass.Text == AccountLogs[i].Password)
{
MessageBox.Show("Welcome " + AccountLogs[i].Account + ".");
gBLogin.Visible = false;
LogCount = 1;
lLbLogin.Text = "Logout";
break;
}
}
catch
{/*
if (LoginAttemptBlock < 5 && LogCount == 0)
{
LoginAttemptBlock++;
int Block = 5;
MessageBox.Show("Login/Pass is incorrect, you can retry it for " + Block + " times else you need to wait for 15minutes to retry");
}
if (LoginAttemptBlock == 5 && LogCount == 0)
{
MessageBox.Show("You need to wait till:" + sa);
}
*/
MessageBox.Show("You entered the wrong username/password make sure that you entered it correctly.");
}
}
}
private void bReg_Click(object sender, EventArgs e)
{
}
private void bCancelLog_Click(object sender, EventArgs e)
{
gBLogin.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
mTBPass.UseSystemPasswordChar = true;
}
private void gBLogin_Enter(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
[/php]
Dont mind this code:
[php]
if (LoginAttemptBlock < 5 && LogCount == 0)
{
LoginAttemptBlock++;
int Block = 5;
MessageBox.Show("Login/Pass is incorrect, you can retry it for " + Block + " times else you need to wait for 15minutes to retry");
}
if (LoginAttemptBlock == 5 && LogCount == 0)
{
MessageBox.Show("You need to wait till:" + sa);
}[/php]
u can use it if want to insert a timer log with attempts, though you'll need to add a SQL table since if you do this passively youll need to create a text file or encrypted file that you have created and reopen it with a timer... i know how to do this in VB for now if you Translated im sure you'll find it):
[php]To add:
[php]
Dim valueshow As String = Convert.ToString(Date.Now.time+TimeSpan.TimeOfDate .Now.MinuteAdd(15)) 'something like
If My.Computer.FileSystem.FileExists("PassiveScan.txt ") = True Then
My.Computer.FileSystem.DeleteFile("PassiveScan.txt ")
My.Computer.FileSystem.WriteAllText("PassiveScan.t xt",valueshow, True)
Else
My.Computer.FileSystem.WriteAllText("PassiveScan.t xt", valueshow, True)
End If
[/php]
to check:
[php]
Dim stringCheck As String = (Date.Now.time)
Dim stringCompareCheck As String =Convert.ToInt32(My.Computer.FileSystem.ReadAllTex t("PassiveScan.txt").ToString
if My.Computer.FileSystem.FileExists("PassiveScan.txt ") = True Then
if stringCheck >= stringCompareCheck then
'codeblocker=0;
else
'Nothing
end if
end if
[/php][/php]
ok to explain the rest:
We have added a LinQ to SQL that we are going to acces we named it"AccountDatabase" to call it in the code you typ:
[php]AccountDatabaseDataContext[/php]
why its named AccountDatabaseDataContext and not AccountDatabase is because of the possibilaty to add things to the database with Contexts.
so the code for creating this object in C# is
[php]AccountDatabaseDataContext ACCDATA = new AccountDatabaseDataContext();[/php]
on to the next code: "List<ACCOUNTSTABLE> AccountLogs = (from Acc in ACCDATA.ACCOUNTSTABLEs orderby Acc.AccountId select Acc).ToList();"
this is easy for creating things like lists, listbox items, combo box, webpartTable items for HTML <TB>, ect.
the concept starts by
[php]List<?> ?[/php]
forcreating a index list
the item that's inside i wrote "ACCOUNTSTABLE" if you might remember we wrote this as the name of the Table we are using.
List<ACCOUNTSTABLE> ?< this is the refractor/variable/name, what ever you like to call it i named it AccountLogs to know what it does you can also name it like "adfokowkaopdk" but idnt do that ;D.
on to this partfrom Acc in ACCDATA.ACCOUNTSTABLEs orderby Acc.AccountId select Acc).ToList()
from ? in ?
[php]from /*(* this is a variable commando)*/a in ?[/php] i now named it a... its like nameing a exception , Catch(Exception a) so now the exception that has been captured will be turned in to the object a inside the entire Catch commandlist, in this target it only for the line its in.
from Acc in ?< this is the object you want to be scearching in and since we are using a Linq to SQL with tables u use <CLASS>.<TABLE>, you can also change this for a listbox or something similair.
from Acc in ACCDATA.ACCOUNTSTABLEs select ?< select is the item you want to use to return a value... in this case we use the item we want to retrieve from FROM <ITEMREF> IN <OBJECT> , SO:
from Acc in ACCDATA.ACCOUNTSTABLEs select Acc, this is what we have i left the Orderby since this aint necesarryly needed only if you want to show a name list in textbox you can choose to do it in order of Numbers/Name/ect.
finally we all need to convert this code to a workable List
so
[php](from Acc in ACCDATA.ACCOUNTSTABLEs orderby Acc.AccountId select Acc).ToList()[/php]
This is why i have added the orderby command:
[php]for (int i = 0; i <= AccountLogs.Count; i++)
{
}[php]
its a for loop a normally if loop can be used though definitions in this kind of loops are small and easy to handle.
its pretty much straight forward...
int i = 0;
if(i<=AccountLogs.Count)
{
}
else
{
AccountLogs.Count
}
this is tolong for a simple usage it does the same as a forloop. sure you can use a while loop but this can be quite long too and this one scans every number its given...though with this one u can go further like this:
[php]
for (int i = 0; i <= AccountLogs.Count; i+=2)
{
textbox1.text+= item[i]
try{textbox1.text+=item[i+1]}catch{/*nothing*/}
textbox1.text+="/n"
}
[/php]
this code:
[php]mTBPass.UseSystemPasswordChar = true;[/php]
is simply to hide the password textbox from showing the text.
This was a tutorial by PSPiso/KuroTenshi/Ryan vd B. it took me 3-4 hours to write + i learned it my self to obtain in 1 day the day after it how to enter sum shit(only was like 3-4 days ago :>).
continue to the next tutorial(ADMIN LOGIN+Register).
Thank you for coming, I'll see you in hell.
@ Anime Section,Otaku/weeabo (orz.) @Graphics Section, Novice DigiArtist
neuest gift from Yura~Chan:
https://bakyurayuu.deviantar*****m/#/d372taw
2nd Place MOTM#9 Theme: CharMods - Combat Arms [No - Thanks] button
come on you know that don't want to push that ordinary button
Using LINQ... erm... but ok..
You should add on how to do it using DataSets and executing Queries.
Light travels faster than sound. That's why most people seem bright until you hear them speak.
@ Anime Section,Otaku/weeabo (orz.) @Graphics Section, Novice DigiArtist
neuest gift from Yura~Chan:
https://bakyurayuu.deviantar*****m/#/d372taw
2nd Place MOTM#9 Theme: CharMods - Combat Arms [No - Thanks] button
come on you know that don't want to push that ordinary button