当我调试我的程序时,它工作正常,直到它:
SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO dbo.Login Values ('" + userNameBox.Text + "','Admin', '" + saltedcryps + "', '" + hashedResult + "')", myConnection);
myCommand.ExecuteNonQuery();
MessageBox.Show("Thanks for registering!");
this.Close();
然后停止,关闭表单和除我的MDI表单之外的所有其他表单。有人可以帮忙吗?
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Data.SqlServerCe;
namespace Event_Control
{
public partial class registerScreen : Form
{
public registerScreen()
{
InitializeComponent();
}
public static string a;
private void registerScreen_Load(object sender, EventArgs e)
{
}
private void cancelButton_Click(object sender, EventArgs e)
{
this.Close();
}
public string radioButtons()
{
if (userButton.Checked)
{
return "User";
}
else if (adminButton.Checked)
{
return "Admin";
}
else
{
MessageBox.Show("You must select an account type");
return "error";
}
}
private bool checkIfUsernameExists()
{
SqlCeConnection myConnection = new SqlCeConnection("Data Source=eventControl.sdf");
try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
string checkuser = "select * from Login where Name = '" + userNameBox.Text + "' ";
SqlCeCommand checkUser = new SqlCeCommand(checkuser, myConnection);
if (checkUser.ExecuteScalar() != null)
{
MessageBox.Show("Username already exists");
return false;
}
else
{
return true;
}
}
private bool checkPasswordsMatch()
{
if (!passwordBox.Text.Equals(confirmPasswordBox.Text))
{
MessageBox.Show("Sorry, your passwords do not match, try again", "Password Error");
passwordBox.Text = "";
confirmPasswordBox.Text = "";
return false;
}
else
return true;
}
private bool checkUserNameEmpty()
{
if (userNameBox.Text == "")
{
MessageBox.Show("Enter a username");
return false;
}
else
return true;
}
private bool checkPasswordsNotEmpty()
{
if (passwordBox.Text == "")
{
MessageBox.Show("Password box is empty");
return false;
}
else
return true;
}
private string saltpassword(int size)
{
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
byte[] buff = new byte[size];
crypto.GetBytes(buff);
return Convert.ToBase64String(buff);
}
private string hashPassAndSalt(string passWithSalt)
{
HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(passWithSalt);
byte[] bytHash = hashAlg.ComputeHash(bytValue);
string base64 = Convert.ToBase64String(bytHash);
return base64;
}
private void registerButton_Click(object sender, EventArgs e)
{
a = radioButtons();
string saltedcryps = saltpassword(10);
string passWithSalt = (passwordBox.Text + saltedcryps);
string hashedResult = hashPassAndSalt(passWithSalt);
if (a == "Admin")
if (checkUserNameEmpty() && checkIfUsernameExists() && checkPasswordsNotEmpty() && checkPasswordsMatch())
{
{
SqlCeConnection myConnection = new SqlCeConnection("Data Source=eventControl.sdf");
try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO dbo.Login Values ('" + userNameBox.Text + "','Admin', '" + saltedcryps + "', '" + hashedResult + "')", myConnection);
myCommand.ExecuteNonQuery();
MessageBox.Show("Thanks for registering!");
this.Close();
}
}
else
{
}
else if (a == "User")
{
if (checkUserNameEmpty() && checkIfUsernameExists() && checkPasswordsNotEmpty() && checkPasswordsMatch())
{
{
SqlCeConnection myConnection = new SqlCeConnection("Data Source=eventControl.sdf");
try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO dbo.Login Values ('" + userNameBox.Text + "','User', '" + saltedcryps + "', '" + hashedResult + "')", myConnection);
myCommand.ExecuteNonQuery();
MessageBox.Show("Thanks for registering!");
this.Close();
}
}
else
{
}
}
}
}
就像我说的那样,它会进入If (a == "admin")
部分的运行命令部分,然后停止工作。当a是User时,它也会这样做。
谢谢你们