我正在开发一个小型信息系统应用程序。我正在使用Windows窗体和基于服务的数据库。它差不多完成但是当我执行应用程序时出现一个非常奇怪的问题。我创建了一个用于在数据库表中插入数据的存储过程,当我手动执行它时,一切正常,但是当我启动程序并通过按钮执行该过程时,发生了错误。乍一看,程序工作正常,程序执行良好,但数据不存储在数据库中。这是我的代码:
UserData.cs
public static bool AddStudent(Student std)
{
UserDataClassesDataContext dc = new UserDataClassesDataContext();
try
{
dc.AddNewStudent(std.FirstName, std.SecondName, std.LastName, std.Faculty, std.Specialty, std.OKS,
std.StudentStatus, std.FacNumber, std.Course, std.Potok, std.Group);
dc.SubmitChanges();
}
catch (Exception)
{
return false;
}
return true;
}
StudentValidation.cs
public bool InsertStudent(Student std)
{
return UserData.AddStudent(std);
}
MainForm.cs buttonClick事件
private void insertStudentButton_Click(object sender, EventArgs e)
{
Student student = new Student();
student.FirstName = tbFirstName.Text;
student.SecondName = tbSecondName.Text;
student.LastName = tbLastName.Text;
student.Faculty = tbFacultyName.Text;
student.Specialty = tbSpecialty.Text;
student.FacNumber = tbFacNumber.Text;
student.OKS = (short)cbOKS.SelectedIndex;
student.StudentStatus = (short)cbStudentStatus.SelectedIndex;
student.Course = (short)numCourse.Value;
student.Potok = tbFlow.Text;
student.Group = tbGroup.Text;
if (sv.InsertStudent(student) && sv.InsertUser(student))
MessageBox.Show("The student is added successfully!");
else
MessageBox.Show("A problem occurs while trying to add the student!");
}
我认为在启动应用程序时db会以某种方式断开连接,但我不知道为什么。
答案 0 :(得分:0)
也许你应该尝试这种方法:
db.students.InsertOnSubmit(STD);
有关详细信息,请参阅此链接 http://msdn.microsoft.com/en-us/library/bb386941.aspx