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
するようにクラスを再設計する必要があり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; }
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加