當我運行我的應用程序時,它會一直到最後,但是當我檢查我的數據庫後,它從不顯示任何數據。這裏是我的代碼:SQL CE不向數據庫輸入信息
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();隱藏();'到你的'try'塊中? – Brian
仍然無法正常工作。 try塊試圖打開連接,如果成功則通過catch塊,然後運行下一個代碼。它基本上是相同的東西 –
在您的bindebug文件夾中查找datbase文件的副本,最好的方法是在連接字符串 – ErikEJ