简体   繁体   English

行未更新

[英]Rows not being updated

I have written the following function meant to update a student in my database: 我写了以下函数来更新我的数据库中的学生:

public bool UpdateStudent(Student stu)
{
   Console.WriteLine("StudentManger.UpdateStudent): Called");

   int rowsChanged = 0;

   using (var cnn = new SqlConnection(
          Properties.Settings.Default.universityConnectionString))
   {
      using (var cmd = new SqlCommand("UPDATE Student " +
              "SET FirstName = @FirstName, " +
              "lastName = @LastName, " +
              "birth = @birth " +
              "WHERE id = @id", cnn))
      {
        cmd.Parameters.Add(new SqlParameter("@FirstName", stu.FirstName));
        cmd.Parameters.Add(new SqlParameter("@LastName", stu.LastName));
        cmd.Parameters.Add(new SqlParameter("@id", stu.ID));
        cmd.Parameters.Add(new SqlParameter("@birth", stu.Birth));

        cnn.Open();
        rowsChanged = (int)cmd.ExecuteNonQuery();
        Properties.Settings.Default.Save();
      }
    }
    Properties.Settings.Default.Save();
    return (rowsChanged != 0);
  }

But when I call function no data is actually getting saved to the Database 但是当我调用函数时,没有数据实际上被保存到数据库中

Can someone tell me why? 有人可以告诉我为什么吗?

With the entire solution and information you provided in chat , your code is fine. 使用您在聊天中提供的整个解决方案和信息,您的代码就可以了。 The problem is that the .mdf database file is set to "Copy to Output Directory": "Always" in your project. 问题是.mdf数据库文件设置为“复制到输出目录”:项目中的“始终”。 Change this property to "Copy if newer" (or "Do not copy" and move it to the bin folder yourself) and it will not overwrite your changes when you re-run the application. 将此属性更改为“如果更新则复制”(或“不复制”并自行将其移动到bin文件夹),并且在重新运行应用程序时不会覆盖您的更改。 Importantly, you will not see the changes you make in your application reflected in your .mdf database file in the project's root directory . 重要的是,您不会看到您在应用程序中所做的更改反映在项目根目录中的.mdf数据库文件 It actually gets copied to the /bin folder and that's where the changes are persisted. 它实际上被复制到/ bin文件夹,并且这是持久化更改的位置。 So, if you don't change the "Copy to Output Directory" property, it will copy from your root to your /bin folder every time you build. 因此,如果您不更改“复制到输出目录”属性,则每次构建时它都会从根目录复制到/ bin文件夹。 Thus it appears that the changes aren't being persisted, when they actually are. 因此, 看起来这些变化并没有持续存在,而实际上却是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM