C#-在没有ORM的情况下处理多对多

时间:2019-05-13 17:14:34

标签: c# wpf many-to-many sqldatareader sqlcommand

我有两个多对多数据库表

tblBook idBook 标题 作家 等等...

tblBookGenre ID idBook idGenre

tblGenre idGenre NameGenre

然后 tblBook idBook 标题 作家 等等...

tblBookTag ID idBook idGenre

tblTag idTag NameTag

我这样映射他们

 public class Book:INotifyPropertyChanged
    {
        Connection con = new Connection();

        private int _idBook;
        private string _title;
        private List<Genre> _genres;
        private List<Tag> _tag;
        private ObservableCollection<Book> _books;

      public int IdBook
        {
            get { return _idBook; }
            set { _idBook = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

       public List<Genre> Genres
        {
            get { return _genres; }
            set { _genres = value; }
        }

        public List<Tag> Tags
        {
            get { return _tag; }
            set { _tag = value; }
        }

        public ObservableCollection<Book> Books
        {
            get { return _books; }
            set { _books = value; }
        }
       public Book()
        {
            Tags = new List<Tag>();
            Books = new ObservableCollection<Book>();
            Genres = new List<Genre>();
        }

我使用sqlcommand和reader从数据库中读取


        public ObservableCollection<Book> GetBooks()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con.con;

            cmd.CommandText = "SELECT idBook,Title FROM tblBook WHERE Active = 1 ORDER BY b.idBook";
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.HasRows)
            {
                while (rd.Read())
                {
                    Book book = new Book();

                    var id = Convert.ToInt32(rd["IdBook"]);
                    var title = rd["Title"].ToString();

                    book.IdBook = id;
                    book.Title = title;

                    List<Tag> tags = GetTagsThatMatch(id);
                    book.Tags = tags;
                    book.Genres = GetGenresThatMatch(id);

                    Books.Add(book);

                }
                rd.Close();
            }
            con.Close();
            return Books;
        }

        public ObservableCollection<Tag> GetTagsThatMatch(int id)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con.con;
            cmd.CommandText = "SELECT t.IdTag,t.Name FROM tblTag t LEFT OUTER JOIN tblBookTag bt ON bt.idTag = t.idTag WHERE bt.idBook = @ID";
            cmd.Parameters.AddWithValue("@ID", id);
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.HasRows)
            {
                while (rd.Read())
                {
                    Tag tag = new Tag();
                    tag.IdTag = Convert.ToInt32(rd["IdTag"]);
                    tag.Name = rd["Name"].ToString();
                    Tags.Add(tag);
                }
            }
            return Tags;
        }

        public List<Genre> GetGenresThatMatch(int id)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con.con;
            cmd.CommandText = "SELECT g.Name FROM tblGenre g LEFT JOIN tblBookGenre bg ON g.IdGenre = bg.IdGenre LEFT OUTER JOIN tblBook b ON bg.IdBook = b.IdBook WHERE b.IdBook = @Id";
            cmd.Parameters.AddWithValue("@ID", id);
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.HasRows)
            {
                while (rd.Read())
                {
                    Genre genre = new Genre();
                    genre.Name = rd["Name"].ToString();
                    Genres.Add(genre);
                }
            }
            return Genres;
        }

问题是它会显示所有书籍的所有流派和标签

书1-类型1,类型2,类型3        -Tag1,Tag2,Tag3

书2-类型1,类型2,类型3        -Tag1,Tag2,Tag3

何时仅显示匹配项

书籍1-类型1,类型2       -Tag1,Tag2

Book 2-Genre3         -Tag3

我真的很想在没有ORM的情况下做到这一点,而且看起来很简单,但是如果没有ORM,我将找不到很多资源。同样,任何文章或示例都将被理解。

0 个答案:

没有答案