行未更新 [英] Rows not being updated

查看:94
本文介绍了行未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了以下函数用于更新我的数据库中的学生:

  public bool UpdateStudent b $ b {
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);
}

但是当我调用函数没有数据实际上被保存到数据库

有人能告诉我为什么吗?

解决方案

您在聊天中提供,您的代码很好。问题是.mdf数据库文件设置为复制到输出目录:总是在您的项目中。将此属性更改为复制(如果是新的)(或不要复制并自行将其移动到bin文件夹),并且在重新运行应用程序时不会覆盖您的更改。重要的是,您不会看到您在应用程序中所做的更改反映在项目根目录中的.mdf数据库文件 中。它实际上被复制到/ bin文件夹,这是更改持久化的地方。因此,如果不更改复制到输出目录属性,它将从您的根目录复制到/ bin文件夹每次构建。因此,出现表示更改并未持久保存。


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. 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. Importantly, you will not see the changes you make in your application reflected in your .mdf database file in the project's root directory. It actually gets copied to the /bin folder and that's where the changes are persisted. 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. Thus it appears that the changes aren't being persisted, when they actually are.

这篇关于行未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆