问题
When i run my application, it goes through to the end just fine, but when i check my DB afterwards, it never shows any data. Here is my code:
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;
}
}
It is the button1_Click that the problem lies in. When it runs myCommand.ExecuteNonQuery(); it never throws an exception, it just carries on, without actually entering any information...
Anyone have a clue??
回答1:
Try this:
if (checkPasswordsMatch() == "B")
{
SqlCeConnection myConnection = new SqlCeConnection("Data Source = pwdb.sdf");
try
{
myConnection.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlCeCommand myCommand = myConnection.CreateCommand();
myCommand.CommandType = CommandType.Text;
myCommand.CommandText = "INSERT INTO PW Values ('Master', '" + saltedcryps + "', '" + hashedResult + "');"
myCommand.ExecuteNonQuery();
myConnection.Close();
this.Hide();
}
If this doesnt works, try to put the absolut path on the connection string.
回答2:
That connection string does not look right; unless you are doing something very very strange you should be using Data Source=|DataDirectory|pwDB.sdf
.
Why do you think your database “never shows any data”? Where are you looking for the data? In the original source data in your project directory? That is almost certainly wrong; when you deploy your application you aren't deploying your source are you? You need to look in the deployment folder, consult this answer.
回答3:
Put the try catch around the ExecuteNonQuery so you can see what is the exception if there, otherwise must be something with your connection string (DataSource=pwDB.sdf doesnt look right to me) must have user Id; Password; dataSource=your IP and initial catalog:
SqlCommand Command = new SqlCommand(YourQuery,myConnection);
myConnection.Open();
int Rows=0;
try
{
Rows = Command.ExecuteNonQuery();
myConnection.Close();
}
catch (Exception ex)
{
conSMS.Close();
string Msg = ex.Message;
//I log my exceptions
//Log("ERROR: "+RemoveSQ(Query));
//Log(RemoveSQ(Msg));
}
//check Rows here if there is no exception
来源:https://stackoverflow.com/questions/19254255/sql-ce-not-entering-information-to-db