How can I modify this code for writing user data to a text file so that it writes to a database.
private
void button1_Click(object sender, EventArgs e)
{
string username = textBox1.Text;
string password = hash_password(textBox2.Text);
using (StreamWriter wr = System.IO.File.AppendText("C:\\pass.txt"))
{
wr.WriteLine(username +
"," + password);
}
this.Close();
}
static string hash_password(string to_be_hashed)
{
byte[] arrbyte = new byte[to_be_hashed.Length];
SHA1 hash = new SHA1CryptoServiceProvider();
arrbyte = hash.ComputeHash(System.Text.
Encoding.UTF8.GetBytes(to_be_hashed));
return Convert.ToBase64String(arrbyte);
}
And this code to read the data:
private
void button1_Click(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader("C:\\pass.txt"))
{
string[] allLines = File.ReadAllLines(@"C:\pass.txt");
string[] usernames = new string[allLines.Length];
string[] passwords = new string[allLines.Length];
for (int i = 0; i < allLines.Length; i++)
{
string[] lineSplit = allLines[i].Split(',');
usernames[i] = lineSplit[0];
passwords[i] = lineSplit[1];
}
string username = textBox1.Text;
string password = hashed_password(textBox2.Text);
for (int i = 0; i < usernames.Length; i++)
if (usernames[i] == username)
if (passwords[i] == password)
{
this.Hide();
Form3 frm = new Form3();
frm.Show();
return;
}
else
{
MessageBox.Show("Incorrect username/password.");
return;
}
MessageBox.Show("Incorrect username/password.");
}
}
static string hashed_password(string to_be_hashed)
{
byte[] arrbyte = new byte[to_be_hashed.Length];
SHA1 hash = new SHA1CryptoServiceProvider();
arrbyte = hash.ComputeHash(System.Text.
Encoding.UTF8.GetBytes(to_be_hashed));
return Convert.ToBase64String(arrbyte);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
I'm really new to programming, and I can understand this code. If modifying this code is just not practical, could someone show me a simple (very simple) example of connecting to a database and storing data in it.