1、操作DBContext类的对象,实现SQL语句的执行=>创建表
using (MyDatabaseEntities db = new MyDatabaseEntities())
{
string sql = "create table ProductUser(id int primary key not null,account varchar(10),password varchar(10))";
var result = db.Database.ExecuteSqlCommand(sql);
Console.WriteLine(result);
Console.ReadKey();
}
在执行数据的增删改时,都可以使用占位符
2、实现数据的添加操作
方法1:
using (MyDatabaseEntities db = new MyDatabaseEntities())
{
string sql = "insert into ProductUser select 2,'zhangsan','555' union select 3,'jeny','666' union select 4,'smith','000'";
var result = db.Database.ExecuteSqlCommand(sql);
Console.WriteLine(result);
Console.ReadKey();
}
方法2:
using (meixinEntities db = new meixinEntities())
{
m_user u = new m_user()
{
email = "157299@qq.com",
pwd = "666",
logintime = DateTime.Now
};
db.m_user.Add(u);
db.SaveChanges();
}
3、实现数据的更新
方法1:
using (MyDatabaseEntities db = new MyDatabaseEntities())
{
string sql = "update ProductUser set password=@pwd1 where account=@acc1;update ProductUser set password=@pwd2 where account=@acc2";
SqlParameter[] para = {
new SqlParameter("@pwd1","abbbbb"),
new SqlParameter("@acc1","tom"),
new SqlParameter("@pwd2","baaaaa"),
new SqlParameter("@acc2","smith")
};
var result = db.Database.ExecuteSqlCommand(sql, para);
Console.WriteLine(result);
Console.ReadKey();
}
方法2:
using (meixinEntities db = new meixinEntities())
{
var result = db.m_user.FirstOrDefault(s => s.email == "efg@qq.com");
result.email = "hahaha@edu.cn";
result.pwd = "6666";
db.SaveChanges();
}
4、实现数据的删除
SQL语言删除数据库中的表:(慎用)
using (MyDatabaseEntities db = new MyDatabaseEntities())
{
string sql = "drop table ProductUser";
var result = db.Database.ExecuteSqlCommand(sql);
}
LINQ删除数据库中的数据:
using (meixinEntities db= new meixinEntities())
{
var result = db.m_user.FirstOrDefault(s => s.email == "abc@163.com");
db.m_user.Remove(result);
db.SaveChanges();
DataBind();
}
总结:一个接口+一个实现
public interface IStudentRepository
{
Student GetStudentById(int id);
IEnumerable<Student> GetAllStudents();
Student Insert(Student student);
Student Update(Student updateStudent);
Student Delete(int id);
}
public class SQLStudentRepository : IStudentRepository
{
private readonly AppDbContext _context;
public SQLStudentRepository(AppDbContext context)
{
_context = context;
}
public Student Delete(int id)
{
Student student = _context.Students.Find(id);
if (student != null)
{
_context.Students.Remove(student);
_context.SaveChanges();
}
return student;
}
public IEnumerable<Student> GetAllStudents()
{
return _context.Students;
}
public Student GetStudentById(int id)
{
return _context.Students.Find(id);
}
public Student Insert(Student student)
{
_context.Students.Add(student);
_context.SaveChanges();
return student;
}
public Student Update(Student updateStudent)
{
var student = _context.Students.Attach(updateStudent);
student.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
_context.SaveChanges();
return updateStudent;
}
}
注意
在appsettings.json配置文件中,添加如下代码,以便读取数据库连接字符串: “ConnectionStrings”: { “WebStudentDBConnection”: “server=(localdb)\MSSQLLocalDB;database=StudentDB;Trusted_Connection=true” }, 该代码localdb表示访问本地数据库,即VS自带数据库,database表示数据库名称,最后一项表示集成windows身份验证连接到sql server。如果使用非本地的SQL Server,则需要更改连接字符串。
在setup类中的依赖注入服务类ConfigureService方法中,连接数据库
services.AddDbContextPool<AppDbContext>(options=>options.UseSqlServer(_configuration.GetConnectionString("WebStudentDBConnection")));
|