将 class 类型添加到数据库

Adding a class type to database

public class Person
{
    public int PersonId { get; set; }

    public string SurName { get; set; }

    public string LastName { get; set; }

    public DateTime DateOfBirth { get; set; }

    public DateTime DateOfDeath { get; set; }

    public Gender gender { get; set; }

    public Person Father { get; set; }

    public Person Mother { get; set; }
}

我想通过查询将父亲和母亲添加到数据库中。

create table Persons(
    PersonId int primary key identity,
    SurName nvarchar(50) not null,
    LastName nvarchar(50) not null,
    DateOfBirth date not null,
    DateOfDeath date null,
    Gender tinyint not null,
    Father ??nvarchar(50) null,
    Mother ??varchar(50) null
);

我不知道用什么作为类型。并且在其他任何地方都找不到它。

public void InsertPerson(Person person)
{
    using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        using (SqlCommand sqlCommand =
             new SqlCommand("insert into Persons values (@surName, @lastName, @dateOfBirth, @dateOfDeath, @gender, @father, @mother);", sqlConnection))
        {
            sqlCommand.Parameters.AddWithValue("@surName", person.SurName);
            sqlCommand.Parameters.AddWithValue("@lastName", person.LastName);
            sqlCommand.Parameters.AddWithValue("@dateOfBirth", person.DateOfBirth);
            sqlCommand.Parameters.AddWithValue("@dateOfDeath", person.DateOfDeath);
            sqlCommand.Parameters.AddWithValue("@gender", person.gender);
            sqlCommand.Parameters.AddWithValue("@father", person.Father);
            sqlCommand.Parameters.AddWithValue("@mother", person.Mother);

            sqlConnection.Open();

            sqlCommand.ExecuteNonQuery();
        }
    }
}

最后我想在数据库中添加值。如果到目前为止是正确的。我假设它不是并且已经问了。 我还在上学,没有我的老师会遇到困难。提前感谢您的帮助:)

您应该重新设计 Person class 以具有 foreign key 参考。

看起来像这样:

public class Person
{
    public int PersonId { get; set; }

    public string SurName { get; set; }

    public string LastName { get; set; }

    public DateTime DateOfBirth { get; set; }

    public DateTime DateOfDeath { get; set; }

    public Gender gender { get; set; }

    public int FatherId { get; set; }

    public int MotherId { get; set; }

 }

然后你的table在数据库中看起来像这样

create table Persons(
    PersonId int primary key identity,
    SurName nvarchar(50) not null,
    LastName nvarchar(50) not null,
    DateOfBirth date not null,
    DateOfDeath date null,
    Gender tinyint not null,
    FatherId int  null,
    MotherId int  null
);

并改变这个 来自:

public Person Father { get; set; }

public Person Mother { get; set; }

收件人:

   public int Father { get; set; }

    public int Mother { get; set; }

如果我想让父亲或母亲成为一名 int,我不确定如何进一步工作。因为爸爸妈妈还是一个人。我正在创建一个家谱程序,最终他们需要能够看到谁是母亲父亲侄女等。

@Vijunav Vastivch System.Data.SqlClient.SqlException: 'The parameterized query '(@surName nvarchar(5),@lastName nvarchar(6),@dateOfBirth datetim' expects the parameter '@father', which was not supplied.'

为什么在创建这样的 table 时需要 ??

 create table Persons(
        PersonId int primary key identity,
        SurName nvarchar(50) not null,
        LastName nvarchar(50) not null,
        DateOfBirth date not null,
        DateOfDeath date null,
        Gender tinyint not null,
        Father ??nvarchar(50) null,
        Mother ??varchar(50) null
    );

Just remove it:
create table Persons(
    PersonId int primary key identity,
    SurName nvarchar(50) not null,
    LastName nvarchar(50) not null,
    DateOfBirth date not null,
    DateOfDeath date null,
    Gender tinyint not null,
    Father nvarchar(50) null,
    Mother varchar(50) null
);

这是设置 Sql 连接的正确方法:

using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        sqlConnection.Open();
        using (SqlCommand sqlCommand =
             new SqlCommand("insert into Persons values (@surName, @lastName, @dateOfBirth, @dateOfDeath, @gender, @father, @mother);", sqlConnection))
        {
            sqlCommand.Parameters.AddWithValue("@surName", person.SurName);
            sqlCommand.Parameters.AddWithValue("@lastName", person.LastName);
            sqlCommand.Parameters.AddWithValue("@dateOfBirth", person.DateOfBirth);
            sqlCommand.Parameters.AddWithValue("@dateOfDeath", person.DateOfDeath);
            sqlCommand.Parameters.AddWithValue("@gender", person.gender);
            sqlCommand.Parameters.AddWithValue("@father", person.Father);
            sqlCommand.Parameters.AddWithValue("@mother", person.Mother);



            sqlCommand.ExecuteNonQuery();
        }
    }