【发布时间】:2013-10-08 17:22:24
【问题描述】:
当我运行我的应用程序时,它会一直运行到最后,但是当我之后检查我的数据库时,它永远不会显示任何数据。这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
string saltedcryps = saltpassword(10);
string passWithSalt = (textBox1.Text + saltedcryps);
string hashedResult = hashPassAndSalt(passWithSalt);
if (checkPasswordsMatch() == "B")
{
SqlCeConnection myConnection = new SqlCeConnection("Data Source = pwdb.sdf");
try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');", myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
this.Hide();
}
}
private string checkPasswordsMatch()
{
if (textBox1.Text == "")
{
MessageBox.Show("Passwords cannot be empty");
return "A";
}
else
{
MessageBox.Show(textBox1.Text == textBox2.Text ? "Thanks for registering!" : "Your passwords do not match");
return "B";
}
}
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;
}
}
问题出在button1_Click上。当它运行myCommand.ExecuteNonQuery();它从不抛出异常,它只是继续,没有实际输入任何信息......
有人知道吗?
【问题讨论】:
-
为什么只在
try块中打开连接?如果将SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');", myConnection); myCommand.ExecuteNonQuery(); myConnection.Close(); this.Hide();移到try块中会发生什么? -
还是不行。 try 块尝试打开连接,如果成功则通过 catch 块然后运行下一个代码。无论哪种方式,它基本上都是一样的
-
在您的bindebug文件夹中查找数据库文件的副本,最好的方法是在连接字符串中使用完整路径
-
是的,但是当我部署它时,它不会工作,因为安装文件夹会有所不同?
-
查看我最新的博文以获取一些建议
标签: c# sql winforms sql-server-ce