在按钮中打印双链接列表

时间:2015-12-14 03:29:48

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

尝试在C#中打印双链表,遇到一些问题,并想知道是否有人可以帮我弄清楚我哪里出错了以及如何在按钮中使用这种打印方法。

这是我的Print方法

public void PrintNodes(LinkedList<Transaction> values)
        {
            if (values.Count != 0)
            {
                Output += "Here are your transaction details:";

                foreach (Transaction t in values)
                {
                    Output += "\r\n" + t;
                }
                Output += "\r\n";
            }
            else
            {
                Output += "The Doubly Linked List is empty!";
            }
        }

这是代码的其余部分

public partial class Form1 : Form
 {
    SqlConnection conn;
    SqlDataAdapter adapter = new SqlDataAdapter();
    SqlCommand command = new SqlCommand();
    public Form1()
    {
        InitializeComponent();
    }

    class Transaction
    {
        SqlConnection conn;
        //variables of the Book Table created
        public int AccountNo;
        public DateTime Date;
        public string Description;
        public string DebitCredit;
        public Double Amount;
        public Transaction(int ACCNo, DateTime TimeDate, string description, string CreditDebit, double amount)
        //constructor
        {
            this.AccountNo = ACCNo;
            this.Date = TimeDate;
            this.Description = description;
            this.DebitCredit = CreditDebit;
            this.Amount = amount;
        }
        public void FillText()
        {
            LinkedList<Transaction> Transactions = new LinkedList<Transaction>(); //create the generic linked list

            SqlConnection conn = new SqlConnection("dbConnection1"); //Connection string

            int AccountNo = Int32.Parse(Microsoft.VisualBasic.Interaction.InputBox("Please enter account number", "Account Number")); //Prompt the user for account number


            SqlCommand cmd = new SqlCommand("Select * From Transactions where AccountNo = " + AccountNo, conn); //command to execute
            conn.Open();  //open the connection to the database           
            SqlDataReader reader = cmd.ExecuteReader();


            if (reader.HasRows)//Check if the table has records
            {
                while (reader.Read()) //read all records with the given AccountNo
                {
                    Transaction Transaction001 = new Transaction(reader.GetInt32(0), reader.GetDateTime(1), reader.GetString(2), reader.GetString(3), reader.GetDouble(4)); //New Transaction node
                    Transactions.AddFirst(Transaction001);// add the node to the Doubly Linked List (Transactions)
                }
            }
            else
            {
                MessageBox.Show("No records found");
            }

            PrintNodes(Transactions);

            reader.Close();
            conn.Close();
        }
        public string Output;
        //This is where the print node is

   }
private void btnExecute_Click(object sender, EventArgs e)
    {

    }

任何帮助都会受到赞赏,因为我非常有信心连接会起作用,但由于某些原因,我无法弄清楚如何打印它来测试它。这可能是一个非常简单的事情,由于某些原因我失踪了,我只是有一个脑屁,但任何帮助将不胜感激。我不是专业人士或学生,只是在玩C#并试图掌握它的人。另外,如果您发现Print方法有任何问题,请告诉我。

0 个答案:

没有答案