如何添加/查找学生详细信息并打印出来

时间:2016-12-23 15:36:38

标签: c#

我想在AddStudent和FindStudent中为StudentData添加代码。

在Program.cs中如何处理注释行中的内容。

这是我的数据库类:

using System;
using System.Data.SQLite;

namespace SampleApp
{
    public class Database
    {
        public static void initializeDatabase()
        {
            var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;");
            dbConnection.Open();

            try
            {
                SQLiteCommand command =
                    new SQLiteCommand(
                        "create table Student (NAME varchar(255), PHONENUMBER varchar(255), ADDRESS varchar(255))",
                        dbConnection);
                command.ExecuteNonQuery();

                command =
                    new SQLiteCommand(
                        "INSERT INTO Student (NAME, PHONENUMBER, ADDRESS) VALUES('Andy Rob','(000) 000-0000', '1500 Logan Drive, Walter, TN')",
                        dbConnection);
                command.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dbConnection.Close();
            }
        }

        public static SQLiteConnection GetConnection()
        {
            var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;");
            dbConnection.Open();

            return dbConnection;
        }

        public static void CleanUp()
        {
            var dbConnection = new SQLiteConnection("Data Source= MyDatabase.sqlite;Version=3;");
            dbConnection.Open();

            try
            {
                SQLiteCommand command =
                    new SQLiteCommand(
                        "drop table Student",
                        dbConnection);
                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dbConnection.Close();
            }
        }
    }
}

这里的学生班

namespace SampleApp
{
    public class Student
    {
        public string name;
        public string phoneNumber;
        public string address;
    }
}

此处的IStudent界面

   public interface IStudent
    {
        Student findStudent(string firstName, string lastName);
        void addStudent(Student newStudent);
    }

此处的学生数据类

namespace SampleApp
{
    public class StudentData : IStudent
    {
        public void AddStudent(Student student)
        {
            throw new System.NotImplementedException();
        }

        public Student findStudent(string firstName, string lastName)
        {
            throw new System.NotImplementedException();
        }
    }
}

此处的计划.cs

class Program
    {
        private StudentData studentData = new StudentData();
        static void Main(string[] args)
        {
            try
            {
                DatabaseUtil.initializeDatabase();
                /* TODO: create student objects and put them in the StudentData and database
                * Ravi, (922) 222-2222, 1411 Tyson Dr, Oak Farms, TN



                // TODO: print the Student Data out to System.out
                // TODO: find Ravi and print out just her entry
                // TODO: insert the new student objects into the database

            }
            finally
            {
                Database.CleanUp();
            }
        }

1 个答案:

答案 0 :(得分:1)

您需要使用SELECT查询来获取具有匹配名称的学生。

SELECT * 
FROM Students
WHERE Name = {0}

请注意,您必须使用您正在寻找的名称String.Format。

但是我建议你研究Entity Framework Core,它负责处理数据库,这意味着你只需要处理对象。