C# 我的应用程序不将数据保存到数据库
我尝试创建一个C应用程序,将数据保存到现有的本地SQL数据库中。我创建了一个名为AccountDatabase.mdf的本地数据库,其中有一个名为AccountTable的表。我首先从一张空桌子开始。然后,我使用下面的代码让应用程序将输入数据保存到数据库中C# 我的应用程序不将数据保存到数据库,c#,sql,.net,C#,Sql,.net,我尝试创建一个C应用程序,将数据保存到现有的本地SQL数据库中。我创建了一个名为AccountDatabase.mdf的本地数据库,其中有一个名为AccountTable的表。我首先从一张空桌子开始。然后,我使用下面的代码让应用程序将输入数据保存到数据库中 private void FPAccountNotExist_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { //Make forgot password p
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;
}
但是,当我在运行应用程序后关闭程序时,我查看了数据库,发现即使在保存了行之后,这些行也没有被保存。请帮我解决这个问题。AccountsDatabase.mdf是您在问题中所说的创建的数据库,例外情况是AccountDatabase.mdf无法访问
如果该文件夹中存在该文件,请检查异常点的路径是否可访问
如果没有,请更改客户端应用程序配置文件中的数据库路径,然后重试。请尝试捕获这些块?我想你没有通过Thrown捕获异常在这种情况下我如何使用try-catch?目前只需将private void CNAOpenAccount\u Clickobject sender,EventArgs e{try{//所有方法的内容}catchException ex{//断点this}如果有错误消息,请告诉我ex中有什么…这是一个附加信息:尝试为文件C:\Users\Joseph\Documents\Visual Studio 2013\Projects\Customer Manager application\Customer Manager application\bin\Debug\AccountDatabase.mdf附加自动命名数据库失败。存在同名数据库,或无法打开指定的文件,或该文件位于UNC共享类型错误消息上。在您给我的路径中是否有.mdf文件?如果没有,请通过编辑客户端项目的配置文件尝试使用正确的路径。我在应用程序中创建的数据库是AccountDatabase.mdf而不是AccountsDatabase.mdf。对于问题中的错误,很抱歉。您是否在C:\Users\Joseph\Documents\Visual Studio 2013\Projects\Customer Manager application\Customer Manager application\bin\Debug\AccountDatabase.mdf中有文件?您指定的文件中没有该文件。@JosephStevens,现在一切正常了吗?您是否更改了配置文件中的paht?
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();
}
}
}