DataAdapter 不删除数据库行

DataAdapter does not delete database row

我正在使用 .Net Framework 3.5 使用 DataSets 和 SQL 服务器开发一个非常古老的项目。所以我需要做点什么。

我在从数据库中删除行时遇到问题。这是我的测试代码:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TypedDataSetTest.UsersDataSetTableAdapters;

namespace TypedDataSetTest
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var userAdapter = new UserTableAdapter())
            {
                var users = userAdapter.GetData();

                userAdapter.DeleteAll();
                users.Clear();    

                PopulateUsers(users, userAdapter);
                PrintData(users);

                var user1 = users.Single(user => user.Name == "user1");
                user1.Delete();
                user1.AcceptChanges();
                //users.AcceptChanges();
                //userAdapter.Update(user1);

                var user2 = users.Single(user => user.Name == "user2");
                user2.Name = "user3";
                //mary.AcceptChanges();
                //users.AcceptChanges();
                //userAdapter.Update(user2);

                userAdapter.Update(users);

                PrintData(users);
            }

            Console.ReadLine();
        }

        private static void PrintData(UsersDataSet.UserDataTable users)
        {
            users.ToList().ForEach(user => Console.WriteLine("{0} {1} {2} {3:dd/MM/yyyy} {4}", user.Id, string.IsNullOrWhiteSpace(user.Field<string>("Name")) ? "" : user.Field<string>("Name"), user.Surname, user.Field<DateTime?>("BirthDate").GetValueOrDefault(), user.TIN));
            Console.WriteLine();
        }


        private static void PopulateUsers(UsersDataSet.UserDataTable users, UserTableAdapter userAdapter)
        {
            var user1 = users.NewUserRow();
            user1.Name = "user1";
            user1.Surname = "user1 surname";
            user1.BirthDate = new DateTime(1970, 12, 03);
            user1.TIN = "123123";

            var user2 = users.NewUserRow();
            user2.Name = "user2";
            user2.Surname = "user2 surname";
            user2.BirthDate = new DateTime(1974, 2, 14);
            user2.TIN = "456456";

            var newUsers = new UsersDataSet.UserRow[] { user1, user2 };
            userAdapter.Connection.Open();

            newUsers.ToList().ForEach(user =>
            {
                if (userAdapter.InsertUser(user) > 0)
                {
                    users.AddUserRow(user);
                    users.AcceptChanges();
                }                
            });

            userAdapter.Connection.Close();
        }
    }
}

所有数据库操作都通过存储过程执行:

CREATE PROCEDURE [dbo].[User_Delete]
    (@Id INT)
AS
    --set nocount on;
    DELETE FROM [dbo].[User] 
    WHERE (Id = @Id);
GO

CREATE PROCEDURE [dbo].[User_DeleteAll]
AS
    SET NOCOUNT ON;

    DELETE FROM [dbo].[User];
GO

CREATE PROCEDURE [dbo].[User_Insert](
    @Name NVARCHAR(50),
    @Surname NVARCHAR(50),
    @BirthDate DATETIM2(7),
    @TIN NVARCHAR(50))
AS
    SET NOCOUNT ON;

    INSERT INTO [dbo].[User] ([Name], [Surname], [BirthDate], [TIN])
    VALUES (@Name, @Surname, @BirthDate, @TIN);

    SELECT SCOPE_IDENTITY();   
GO

CREATE PROCEDURE [dbo].[User_SelectAll]
AS 
    SET NOCOUNT ON;

    SELECT    
        [Id], [Name], [Surname], [BirthDate], [TIN]
    FROM 
        [dbo].[User];
GO

CREATE PROCEDURE [dbo].[User_Update]
    (@Id INT,
     @Name NVARCHAR(50),
     @Surname NVARCHAR(50),
     @BirthDate DATETIME2(7),
     @TIN NVARCHAR(50))
AS
     SET NOCOUNT ON;

     UPDATE [dbo].[User]
     SET [Name] = @Name,
         [Surname] = @Surname,
         [BirthDate] = @BirthDate,
         [TIN] = @TIN
     WHERE (Id = @Id)
GO

这里还有我的 xsd 和 table-适配器信息,都是按照书本的(我认为):

此代码似乎在控制台上有效,user1 已从 DataTable 中删除,user2 名称已更改为 "user3"。

但在数据库table中,只有名称的更改被保留。 User1 没有被删除。

如果我调用 userAdapter.Update(user1); 一切都很好。我已经尝试了在 Internet 上找到的几种方法,但似乎没有任何效果。

看起来 userAdapter 无法找到 user1 行的 DeleteCommand。没有例外,只是没有从数据库中删除而已。

我错过了什么吗?我做错了什么吗?

我发现了问题。此代码工作正常:

using (var userAdapter = new UserTableAdapter())
            {
                var users = userAdapter.GetData();

                userAdapter.DeleteAll();
                users.Clear();    

                PopulateUsers(users, userAdapter);
                PrintData(users);

                var user1 = users.Single(user => user.Name == "user1");
                var user2 = users.Single(user => user.Name == "user2");

                user1.Delete(); 
                user2.Name = "user3";

                userAdapter.Update(users);

                PrintData(users);
            }

在之前的版本中,如果user1是"marked" as to-delete,我无法找到user2,所以我调用了AcceptChanges来迭代通过usersdata-table得到user2。调用 user1.AcceptChanges(); 更改了 user1 行的状态,因此没有 to-delete 信息。

所以一切都很好!