Trying to add data from SQL database and print into DoublyLinked List

时间:2015-12-10 02:00:43

标签: c# class linked-list nodes doubly-linked-list

I am just playing with C# and I am trying to add data from an SQL database to a Doubly Linked List and I am having trouble with printing this using a button. Just want to know how to properly print and how to use this in a button.

public class Book
    {
        SqlConnection conn;
        //variables of the Book Table created
        public int ISBNno;
        public string Title;
        public string Category;
        public string Acode;
        public Book(int ISBN, string title, string category, string
       acode)
        //constructor
        {
            this.ISBNno = ISBN;
            this.Title = title;
            this.Category = category;
            this.Acode = acode;
        }
    }

    public class Structures
    {
        SqlConnection conn;
        public void LoadData()
        {
            LinkedList<Book> book = new LinkedList<Book>(); // doubly linked list
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings[@"dbConnection1"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Select * From Book where ISBNno = 123456789", conn); // command to execute
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())// read all records with the ISBNno 123456789
            {
                Book theBook = new Book(reader.GetInt32(0),
               reader.GetString(1), reader.GetString(2), reader.GetString(3));
                //new Book node
                book.AddFirst(theBook); // add it to the Doubly Linked List
            }
            foreach (Book t in values)
            {
                //Formatting code needed for tabbed appearance
                Output += "\r\n" + t.Title + "\t" + t.Category;
                //etc...
            }
            return Output += "\r\n"; 
            reader.Close();
            conn.Close();               
        }
        public string Output;
        public String PrintNode(LinkedList<Book> values) // print nodes
        {
            foreach (Book t in values)
            {
                //Formatting code needed for tabbed appearance
                Output += "\r\n" + t.Title + "\t" + t.Category;
                //etc...
            }
            return Output += "\r\n"; 
        }
    }

Think my connection works but hard to tell as I cannot print. Any help would be appreciated.

0 个答案:

没有答案