using StudengManage. Models ;
using System ;
using System. Collections. Generic ;
using System. Data. SqlClient ;
namespace StudengManage. DAL
{
public class StudentService
{
public int addStudent ( Student student)
{
string insertSql = "insert into Student(StudentId,StudentName,Gender,Birthday,studentIdNo,Age,PhoneNumber,StudentAddress,ClassId) " +
"values({0},'{1}','{2}','{3}',{4},{5},'{6}','{7}',{8})" ;
insertSql = string . Format ( insertSql, student. StudentId, student. StudentName, student. Gender, student. Birthday, student. studentIdNo, student. Age
, student. PhoneNumber, student. StudentAddress, student. ClassId) ;
return SqlHelper. add ( insertSql) ;
}
public Student queryStudentById ( int stuId)
{
string querySql = "select * from Student where StudentId = {0}" ;
querySql = string . Format ( querySql, stuId) ;
SqlDataReader sqlData = SqlHelper. query ( querySql) ;
Student stu = null ;
if ( sqlData. Read ( ) )
{
stu = new Student ( )
{
StudentName = sqlData[ "StudentName" ] . ToString ( ) ,
Birthday = Convert. ToDateTime ( sqlData[ "Birthday" ] . ToString ( ) )
} ;
}
sqlData. Close ( ) ;
return stu;
}
public List< Student> queryAllStudens ( )
{
string querySql = "select studentid,studentname,gender,birthday from student" ;
SqlDataReader sqlData = SqlHelper. query ( querySql) ;
List< Student> list = new List< Student> ( ) ;
while ( sqlData. Read ( ) )
{
list. Add ( new Student ( )
{
StudentName = sqlData[ "StudentName" ] . ToString ( ) ,
Birthday = Convert. ToDateTime ( sqlData[ "Birthday" ] . ToString ( ) )
} ) ;
}
sqlData. Close ( ) ;
return list;
}
}
}
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
using System. Data ;
using System. Data. SqlClient ;
using System. Data. Sql ;
namespace StudengManage. DAL
{
public class SqlHelper
{
private static string connStr = "server = localhost;database = MyPhoneList; uid = 123; pwd = 123;" ;
public static int add ( string sql) {
SqlConnection sqlCon = new SqlConnection ( connStr) ;
SqlCommand sqlCom = new SqlCommand ( sql, sqlCon) ;
sqlCon. Open ( ) ;
int result = sqlCom. ExecuteNonQuery ( ) ;
sqlCon. Close ( ) ;
return result;
}
public static SqlDataReader query ( string sql) {
SqlConnection sqlCon = new SqlConnection ( connStr) ;
SqlCommand sqlcom = new SqlCommand ( sql, sqlCon) ;
sqlCon. Open ( ) ;
SqlDataReader sqlData = sqlcom. ExecuteReader ( CommandBehavior. CloseConnection) ;
return sqlData;
}
}
}
using System ;
namespace StudengManage. Models
{
public class Student
{
public int StudentId { get ; set ; }
public string StudentName { get ; set ; }
public string Gender { get ; set ; }
public DateTime Birthday { get ; set ; }
public decimal studentIdNo { get ; set ; }
public int Age { get ; set ; }
public string PhoneNumber { get ; set ; }
public string StudentAddress { get ; set ; }
public int ClassId { get ; set ; }
}
}
using StudengManage. DAL ;
using StudengManage. Models ;
using System ;
using System. Collections. Generic ;
namespace StudengManage
{
internal class Program
{
private static void Main ( string [ ] args)
{
StudentService stuSer = new StudentService ( ) ;
List< Student> list = stuSer. queryAllStudens ( ) ;
foreach ( Student stu in list)
{
Console. WriteLine ( "学生姓名:" + stu. StudentName + "\t学生学号" + stu. StudentId) ;
}
Console. ReadLine ( ) ;
}
}
}