C# 如何添加/查找学生详细信息并打印
我想在AddStudent和FindStudent中向StudentData添加代码 在Program.cs中,如何在注释行中执行该操作 这是我的数据库类:C# 如何添加/查找学生详细信息并打印,c#,C#,我想在AddStudent和FindStudent中向StudentData添加代码 在Program.cs中,如何在注释行中执行该操作 这是我的数据库类: using System; using System.Data.SQLite; namespace SampleApp { public class Database { public static void initializeDatabase() { var db
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);
}
这是StudentData类
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();
}
}
}
这里是Program.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();
}
}
您需要使用SELECT查询来获取具有匹配名称的学生
SELECT *
FROM Students
WHERE Name = {0}
请注意,您必须使用字符串。使用您要查找的名称格式化该字符串
不过,我建议您研究一下,它负责处理数据库,这意味着您只需要处理对象。这样您就知道如何编写(或获得)
CREATE TABLE
和INSERT-into-SQL
语句了-现在对SELECT
进行一些研究。或者,如果这不是问题所在,请准确地添加您所坚持的内容。“我现在做什么?”对于这个论坛来说是一个太模糊的问题。上面更新了这个问题即使在更新之后,这个问题也太模糊了,答案很可能是“学会如何使用选择
”。现在我已经更新了Kay,所以你有四个任务要做,而且没有显示任何尝试或研究证据。因此,这不是一个培训网站——在这个网站上有很多例子,还有很多其他的例子可以帮助你了解你需要做什么。在你展示了自己的尝试(甚至失败的尝试)之后,带着特定的问题回来。如何添加学生和查找学生你已经知道如何添加学生(插入到
),我介绍了如何查找学生。如果您还有任何概念性问题,我很乐意提供帮助,但您现在应该能够自己实现这些方法。