-1

当我运行我的应用程序时,它会一直运行到最后,但是当我之后检查我的数据库时,它从不显示任何数据。这是我的代码:

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

将 try catch 放在 ExecuteNonQuery 周围,这样您就可以看到是否存在异常,否则必须与您的连接字符串有关(DataSource=pwDB.sdf 在我看来不正确)必须具有用户 ID;密码; 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 回答