繁体   English   中英

用数据库填充双向链表并显示

[英]Populate doubly linked list with Database and display

我一直在使用C#中的SQL和双向链接列表进行操作,我想知道是否有人知道一种方法来显示我创建的SQL Server文件中的信息,该文件通过提示用户输入帐号并显示来显示假银行交易。它在双链表中。

public class Transaction
    {
        SqlConnection conn;
        //variables of the Book Table created
        int AccountNo;
        DateTime Date;
        string Description;
        string DebitCredit;
        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;
        }
        //My current print method
        public string PrintNodes(LinkedList<int> 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!";
            }
        }
    }
//my method for taking the information from database and pasting into DLL
    public void FillText(LinkedList<Transaction> values)
        {
            LinkedList<Transaction> Transactions = new LinkedList<Transaction>(); //create the generic linked list

            conn = new SqlConnection(ConfigurationManager.ConnectionStrings[@"dbConnection1"].ConnectionString); //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");
            }

            txtOutput.Text = Transactions.PrintNodes();

            reader.Close();
            conn.Close();
        }

我希望它按照以下方式显示:

Transactions for account 12345678

Date              Description              Debit/Credit       Amount

01/01/2015        Hello eofcoef            C                  $1000.00

首先,查看代码中的职责划分。 您已定义对象Transaction,该对象用于描述事务。 但是,现在您已在其中添加了其他事务的列表以及在此列表中显示事务的方法。 将其移至另一个类,因为您希望事务是统一的和原子的,这意味着它只是对事务的描述。

然后,您将拥有一个用于处理和处理事务组的不同类型的对象。 您可以更改FillText方法以返回事务,带有此关键字的返回,实例化的对象(如构建器模式)。 这样,您可以在外部初始化连接,然后断开PrintNode,因为那不属于Transaction-object。

对于打印,您正在打印对象引用本身。

 foreach (Transaction t in values)
            {
                Output += "\r\n" + t;
            }
            Output += "\r\n";

您需要取消引用所需的属性,如下所示:

 foreach (Transaction t in values)
            {
                //Formatting code needed for tabbed appearance
                Output += "\r\n" + t.Description + "\t" + t.DebitCard;     
                //etc...


            }
            Output += "\r\n"; 

您也可以在Transaction类中重写ToString(),以将其作为其名称来引用(假设您提供了从此重写方法返回的自定义字符串),例如:

public override string ToString(){ //Return a formatted string here }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM