Jump to content

[HELP] - C sharp Database

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Unskipp

Unskipp

    Newbie

  • Members
  • Pip
  • 5 posts
Hello World . I`m kinda newbie at c sharp ... i`m learning it at school for about one year and i need help for a school project .

Well ... What i wanna do is to make a database with usernames and passwords .
I`ve made a program that adds a new line in the database but i don`t know how to save it and edit the new row.

So the dataset name is : Proiect
Table`s name : Table1
The line where i wanna add the username is : Username
The line where i wanna add the password is : Password
Both in table Table1

Also i have a TextBox for username and anotherone for password and a button wich should add them to the database , but it doesn`t . I got it working once but i didn`t save it and i forgot how i made .

Here`s my code so far :

Quote

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
proiectDataSet.Table1Row newrow;
newrow = proiectDataSet.Table1.NewTable1Row ();
proiectDataSet.Table1.Rows.Add(newrow);
this.table1TableAdapter.Update(this.proiectDataSet.Table1);
}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'proiectDataSet.Table1' table. You can move, or remove it, as needed.
this.table1TableAdapter.Fill(this.proiectDataSet.Table1);

}


}
}

i didn`t make the code for the password yet becouse i`ll do it after i get the username thing working .

Thanks a lot :rolleyes:

#2
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
What kind of database are you using? MySQL, MS Access, or something else?

Or might it be just a file that your program creates? In that case the only way to edit an existing line is to completly rewrite the entire file because what's on your hard drive can not be expanded or contracted without rewriting it. You can add new records (or rows) to the end of the file but not anywhere in the middle.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#3
Unskipp

Unskipp

    Newbie

  • Members
  • Pip
  • 5 posts
hello , thanks for reply

well ... the database is created from C sharp visual studio so it is a "service - based DataBase" , an SQL server DataBase .

#4
Unskipp

Unskipp

    Newbie

  • Members
  • Pip
  • 5 posts
i have managed to edit the new row but still don`t know how to save it to database

#5
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts

Unskipp said:

i have managed to edit the new row but still don`t know how to save it to database

Use the adapter.Update command again.

#6
Unskipp

Unskipp

    Newbie

  • Members
  • Pip
  • 5 posts
allright , i decided to create a Microsoft Acces database and use it in my project in c# and it worked . i managed to create a new row , to edit it and also to save the new row into database .

now ... what i wanna do is to add a new textbox and a button wich will check if the username from new textbox matches with any username from the database . i`m really stuck here :D please help

#7
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts

Unskipp said:

allright , i decided to create a Microsoft Acces database and use it in my project in c# and it worked . i managed to create a new row , to edit it and also to save the new row into database .

now ... what i wanna do is to add a new textbox and a button wich will check if the username from new textbox matches with any username from the database . i`m really stuck here :D please help

Create yourself a table that will store the username and password hashes (Use SHA hashes for security)

When the user clicks the button, get the user name, hash the password, then run a query (use ODBCCommand object, should support basic SQL syntax) to check for the existence of the username/password.

#8
Unskipp

Unskipp

    Newbie

  • Members
  • Pip
  • 5 posts
well , like i said in my first post , i`m kinda newbie at c# and i don`t know how to make a query in database . there is no other possibility to check if username and password exist in database , like a code in eventhandler of the button ?

#9
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts

Unskipp said:

well , like i said in my first post , i`m kinda newbie at c# and i don`t know how to make a query in database . there is no other possibility to check if username and password exist in database , like a code in eventhandler of the button ?

Well, the code will be in the Click event handler for the button (or another method that is called from it)

So your code will look something like this:


private void Button1_Click

{

[INDENT]

string strUserName = TextBox1.Text;

Byte[] originalPassword;

Byte[] hashedPassword;

Sha256 sha;


//Instantiate SHA256CryptoServiceProvider, get bytes for original password and compute hash (encoded password)

sha1 = new SHA256CryptoServiceProvider();

originalBytes = UTF8Encoding.UTF8.GetBytes(TextBox2.Text);

encodedBytes = sha1.ComputeHash(originalBytes);



//Convert encoded bytes back to a 'readable' string

string strPassword = BitConverter.ToString(encodedBytes, 0);


OdbcConnection conn = new OdbcConnection("Connection string for access goes here. Google it");


OdbcCommand query = conn.CreateCommand();


query.CommandText = "SELECT userName FROM userPassTable WHERE userName = @userName AND passHash = @passHash";

query.Parameters.AddWithValue("@userName", strUserName);

query.Parameters.AddWithValue("@passHash", strPassword);


OdbcDataReader reader = query.ExecuteReader();


if(reader.Read())

{

[INDENT]"Successful Login"[/INDENT]

}

else

{

[INDENT]"Bad login!"[/INDENT]

}

[/INDENT]

}


So, that's the basics... There may be some small mistakes there, but I just wrote the code. I've not tested it. This assumes that TextBox1 is your user name text box, TextBox2 is your password text box, and that you're storing the hashes in the table, not the actual passwords. If you want to store the hashes in the table, you can do the same thing, only use an insert statement at the SQL part. SQL isn't that difficult, you can find some tutorials online. This should get you headed in the right direction anyway.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users