-1

アプリケーションを実行すると、最後まで問題なく実行されますが、後で DB を確認すると、データが表示されません。これが私のコードです:

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(); を実行すると、例外をスローすることはなく、実際に情報を入力せずに続行します...

誰にも手がかりがありますか??

4

3 に答える 3

0

これを試して:

    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();

        }

これが機能しない場合は、接続文字列に絶対パスを配置してみてください。

于 2013-10-08T17:46:23.853 に答える
0

その接続文字列は正しくないようです。非常に奇妙なことをしている場合を除き、 を使用する必要がありますData Source=|DataDirectory|pwDB.sdf

データベースに「データがまったく表示されない」のはなぜだと思いますか? どこでデータを探していますか? プロジェクト ディレクトリの元のソース データに? それはほぼ確実に間違っています。アプリケーションをデプロイするとき、ソースをデプロイしていませんか? 展開フォルダーを調べる必要があります。この回答を参照してください。

于 2013-10-08T17:50:24.903 に答える
0

ExecuteNonQuery の周りに try キャッチを配置して、例外がある場合は何が例外であるかを確認できるようにします。パスワード; dataSource=あなたの IP と初期カタログ:

        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
于 2013-10-08T18:08:08.643 に答える