【问题标题】:Adding a class type to database将类类型添加到数据库
【发布时间】:2020-01-02 06:42:44
【问题描述】:
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();
        }
    }
}

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

【问题讨论】:

  • 你需要一个外键,基本上母亲和父亲列将是其他Persons的主键
  • 运行此代码时是否返回任何错误?如果有,请在此处显示错误。为什么你需要使用这个public Person Father { get; set; } public Person Mother { get; set; } 而不是使用像public string Father 这样的普通数据类型?

标签: c# database class sqlcommand


【解决方案1】:

您应该重新设计Person 类,使其具有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; }

 }

那么你在数据库中的表是这样的

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; }

【讨论】:

    【解决方案2】:

    如果我将父亲或母亲设为 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.'

    【讨论】:

    【解决方案3】:

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

     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
    );
    

    【讨论】:

      【解决方案4】:

      这是设置 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();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2011-09-24
        • 1970-01-01
        • 1970-01-01
        • 2020-04-28
        • 1970-01-01
        • 2012-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多