我試圖創建一個將數據保存到現有本地SQL數據庫的C#應用程序。我創建了一個名爲AccountDatabase.mdf
的本地數據庫,其中有一個名爲AccountTable
的表。我首先從一張空桌子開始。然後,我使用下面的代碼讓應用程序將輸入數據保存到數據庫。我的應用程序不會將數據保存到數據庫中
private void FPAccountNotExist_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
//Make forgot password page invisible and create new account page visible
ForgotPassword.Visible = false;
CreateNewAccount.Visible = true;
//Set text content of labels on create new account page
CreateNewAccountTitle.Text = "Create New Account";
CNAConfirmLabel.Text = "Confirm Password";
CNAEmailLabel.Text = "Email";
CNAInstruction.Text = "Please fill in the following details.";
CNAOpenAccount.Text = "Submit new account";
CNAPasswordLabel.Text = "Password";
CNAUsernameLabel.Text = "Username";
OpenManual.Text = "Start with Manual";
CNAConfirmTextBox.UseSystemPasswordChar = true;
CNAPasswordTextBox.UseSystemPasswordChar = true;
}
private void CNAOpenAccount_Click(object sender, EventArgs e)
{
AccountVariables.ConfirmedPassword = CNAConfirmTextBox.Text;
AccountVariables.Email = CNAEmailTextBox.Text;
AccountVariables.Password = CNAPasswordTextBox.Text;
AccountVariables.Username = CNAUsernameTextBox.Text;
//GeneralVariables.ManualOption = OpenManual.Checked;
var CreateNewAccountStrings = new List<string> { AccountVariables.ConfirmedPassword, AccountVariables.Password, AccountVariables.Email, AccountVariables.Username };
if (CreateNewAccountStrings.Contains(""))
{
MessageBox.Show("All textboxes should be filled");
}
else
{
if ((AccountVariables.ConfirmedPassword == AccountVariables.Password) && (AccountVariables.Password.Length >= 8))
{
accountTableTableAdapter.Fill(accountDatabaseDataSet.AccountTable);
AccountVariables.defaultAccountRow = AccountVariables.defaultAccountRow + 1;
AccountVariables.newAccountTableRow = accountDatabaseDataSet.AccountTable.NewAccountTableRow();
AccountVariables.newAccountTableRow.UserId = AccountVariables.defaultAccountRow;
AccountVariables.newAccountTableRow.Username = AccountVariables.Username;
AccountVariables.newAccountTableRow.Email = AccountVariables.Email;
AccountVariables.newAccountTableRow.Password = AccountVariables.Password;
accountDatabaseDataSet.AccountTable.AddAccountTableRow(AccountVariables.newAccountTableRow);
accountTableTableAdapter.Update(accountDatabaseDataSet.AccountTable);
accountDatabaseDataSet.AccountTable.AcceptChanges();
AccountVariables.AccountDatabaseConnection.Close();
}
}
}
然而,當我運行該應用程序後,關閉程序時,我看着在數據庫中,只見那行沒有被保存,即使我有救了。請你能幫我解決這個問題。
可以請你試試看看這些塊嗎我認爲你沒有發現異常拋出 – clement 2014-12-05 12:02:00
我怎樣才能在這種情況下使用try catch? – 2014-12-06 14:06:29
此刻只是把private void CNAOpenAccount_Click(object sender, EventArgs e) {try {//所有方法的內容} catch(Exception ex){// breakpoint this}}並告訴我ex前面的內容是否爲錯誤消息一個...... – clement 2014-12-08 07:39:55