2014-12-05 79 views
0

我試圖創建一個將數據保存到現有本地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(); 
     } 
    } 
} 

然而,當我運行該應用程序後,關閉程序時,我看着在數據庫中,只見那行沒有被保存,即使我有救了。請你能幫我解決這個問題。

+0

可以請你試試看看這些塊嗎我認爲你沒有發現異常拋出 – clement 2014-12-05 12:02:00

+0

我怎樣才能在這種情況下使用try catch? – 2014-12-06 14:06:29

+0

此刻只是把private void CNAOpenAccount_Click(object sender, EventArgs e) {try {//所有方法的內容} catch(Exception ex){// breakpoint this}}並告訴我ex前面的內容是否爲錯誤消息一個...... – clement 2014-12-08 07:39:55

回答

0

AccountsDatabase.mdf是您創建的數據庫(如您在您的問題中所述),並且該例外說明AccountDatabase.mdf無法訪問。

請檢查路徑異常點是可到達的(如果是文件夾中的文件。

如果沒有,請更改是在客戶端的應用程序的配置文件中的數據庫的路徑,然後重試

+0

我創建的數據庫是我的應用程序中的AccountDatabase.mdf而不是AccountsDatabase.mdf。 – 2014-12-08 11:24:01

+0

你在C:\ Users \ Joseph \ Documents \ Visual中有文件嗎? Studio 2013 \ Projects \ Customer Manager application \ Customer Manager application \ bin \ Debug \ AccountDatabase.mdf? – clement 2014-12-08 11:26:33

+0

它不在您指定的文件中。 – 2014-12-08 11:33:36

相關問題