using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class StudentInfo
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Phone { get; set; }
public string Hobby { get; set; }
}
}
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class StudentInfoService
{
//1.查询所有数据的方法
public static List<StudentInfo> GetStudentInfos()
{
//查询语句
string sql = "select * from StudentInfo";
//借助DBHelper查询
DataTable dt =DBHelper.ExecuteTable(sql);
//创建集合
List<StudentInfo> list = new List<StudentInfo>();
foreach (DataRow dr in dt.Rows)
{
StudentInfo stu = new StudentInfo();
stu.ID =int.Parse( dr[0].ToString());
stu.Name = dr[1].ToString();
stu.Gender = dr[2].ToString();
stu.Phone = dr[3].ToString();
stu.Hobby = dr[4].ToString();
list.Add(stu);
}
return list;//返回集合
}
//2.模糊查询的方法
public static List<StudentInfo> GetStudentInfoByName(string key)
{
string sql = string.Format("select * from StudentInfo where Name like '%{0}%'",key);
//借助DBHelper查询
DataTable dt = DBHelper.ExecuteTable(sql);
//创建集合
List<StudentInfo> list = new List<StudentInfo>();
foreach (DataRow dr in dt.Rows)
{
StudentInfo stu = new StudentInfo();
stu.ID = int.Parse(dr[0].ToString());
stu.Name = dr[1].ToString();
stu.Gender = dr[2].ToString();
stu.Phone = dr[3].ToString();
stu.Hobby = dr[4].ToString();
list.Add(stu);
}
return list;//返回集合
}
//3.删除的方法
public static int DeleteStudentInfo(int id)
{
string sql = "delete from StudentInfo where ID=" + id;
return DBHelper.ExecuteQuery(sql);//返回受影响行数
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public static class DBHelper
{
static string connstr = "server=.;database=StudentDB;uid=sa;pwd=1234";
/// <summary>
/// 执行T-SQL返回一张表信息
/// </summary>
/// <param name="sqlStr">T-SQL查询语句</param>
/// <returns></returns>
public static DataTable ExecuteTable(string sqlStr)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(sqlStr, conn))
{
SqlDataAdapter sda = new SqlDataAdapter(comm);
DataSet set = new DataSet();
sda.Fill(set);
return set.Tables[0];
}
}
}
/// <summary>
/// 执行T-SQL完成增删改的操作
/// </summary>
/// <param name="sqlStr">T-SQL查询语句</param>
/// <returns></returns>
public static int ExecuteQuery(string sqlStr)
{
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(sqlStr, conn))
{
return comm.ExecuteNonQuery();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using DAL;
namespace BLL
{
public class StudentInfoManager
{
public static List<StudentInfo> GetStudent()
{
return StudentInfoService.GetStudentInfos();
}
public static List<StudentInfo> GetStudentByName(string key)
{
return StudentInfoService.GetStudentInfoByName(key);
}
public static int DeleteStudentInfo(int id)
{
return StudentInfoService.DeleteStudentInfo(id);
}
}
}