asp.net core 2.1 dotnet(三)EF.core 的增,删,改,查

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xiajing13579/article/details/82834480

1.调用方式:

   增加引用: 

        using Microsoft.EntityFrameworkCore;

        using Newtonsoft;

增加加载的类构造函数:

    APPDbContext _context;

    public ValuesController(APPDbContext context){

    this._context = context;

   }

2.在appsettings.json中增加 配置 如下:

{

"ConnectionStrings": {

"SqlServerConnection": "Server=.;Database=dbCore;User ID=sa;Password=password;"

},

"Logging": {

"LogLevel": {

"Default": "Warning"

}

},

"AllowedHosts": "*"

}

3.增加 APPDbContext类

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using Microsoft.EntityFrameworkCore;

using CoreAPI.Mdoel;

namespace CoreAPI

{

public class APPDbContext:DbContext

{

 

public APPDbContext(DbContextOptions<APPDbContext> options):base(options)

{

 

}

public DbSet<User> User { get; set; }

 

protected override void OnModelCreating(ModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder);

}

}

}

4.增加的例子:

这个和调用别EF 的方式一样。

CoreAPI.Mdoel.User user = new CoreAPI.Mdoel.User();

user.UserName = "tesd2";

user.Password = "password";

_context.Add(user);

_context.SaveChanges();

5. 修改的例子:

var v1 = _context.User.Where(c=>c.Id==2); //得到数据库中的一行。

User user = v1.Take(1).Single();

user.password = "test";

_context.SaveChanges();

6.删除的例子

var v1 = _context.User.Where(c=>c.Id==2);

CoreAPI.Mdoel.User user1= v1.Take(1).Single();

_context.User.Remove(user1);

_context.SaveChanges();

7.查找的例子

var v2 = from t in _context.User select t;

foreach(var item in v2){

Console.Write(item.Id.ToString()+":"+ item.UserName);

}

8.事务,这个还没有试,稍后再加上

展开阅读全文

xml 增 删 改 查

11-03

xml文件格式rnrnrn rn ewrn dsarn eqrn rnrn[code=C#]rn private void btnCreateXml_Click(object sender, System.EventArgs e)rn rn //创建XML文档对象rn System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();rn //创建xml 声明节点rn System.Xml.XmlNode xmlnode = xmldoc.CreateNode(System.Xml.XmlNodeType.XmlDeclaration, "", "");rn //添加上述创建和 xml声明节点rn xmldoc.AppendChild(xmlnode);rn //创建xml dbGuest 元素(根节点)rn System.Xml.XmlElement xmlelem = xmldoc.CreateElement("", "dbGuest", "");rnrn xmldoc.AppendChild(xmlelem);rn tryrn rn xmldoc.Save(xmlFile);rn rn catch(Exception ex)rn rn if(ex.Message.IndexOf("访问被拒绝") != -1)rn rn lblMsg.Text = "创建xml文档失败,没有写入权限,请检查xmldocs文件夹的写入权限!";rn return;rn rn rn lblMsg.Text = "xml文件创建成功";rn BindXMLFiles();rn GetXmlContent();rn BindUserNames();rn rn rnprivate void btnAdd_Click(object sender, System.EventArgs e)rn rn string uID = txtUID.Text;rn string userName = txtUserName.Text;rn string City = txtCity.Text;rn string Email = txtEmail.Text;rn if(!System.IO.File.Exists(xmlFile))rn rn lblMsg.Text = "xml文件"+xmlFile+"不存在,请先点击创建!";rn return;rn rn XmlDocument xmlDoc = new XmlDocument();rn xmlDoc.Load(xmlFile); //加载文件rn XmlNode root = xmlDoc.SelectSingleNode("dbGuest");//查找rn XmlElement xe_dbGuest_User = xmlDoc.CreateElement("User");//创建一个节点rn XmlElement xe_dbGuest_User_Name = xmlDoc.CreateElement("Name");rn xe_dbGuest_User_Name.InnerText = userName;//设置文本节点rn rn XmlAttribute a = xmlDoc.CreateAttribute("uid");//创建一个 属性rn a.Value = uID;rn xe_dbGuest_User_Name.Attributes.Append(a);//增加一个属性rn xe_dbGuest_User.AppendChild(xe_dbGuest_User_Name);//添加到节点中rn XmlElement xe_dbGuest_User_City = xmlDoc.CreateElement("City");//创建City节点rn xe_dbGuest_User_City.InnerText = City;rn xe_dbGuest_User.AppendChild(xe_dbGuest_User_City);//添加到节点中rn XmlElement xe_dbGuest_User_Email = xmlDoc.CreateElement("Email");//创建Email节点rn xe_dbGuest_User_Email.InnerText = Email;rn xe_dbGuest_User.AppendChild(xe_dbGuest_User_Email);rnrn root.AppendChild(xe_dbGuest_User);//添加到节点中rn xmlDoc.Save(xmlFile);//保存文件rn lblMsg.Text = "成功添加用户";rn GetXmlContent();rn BindXML();rn BindUserNames();rn rn//绑定xml数据rn private void BindXML()rn rn if(!System.IO.File.Exists(xmlFile))rn rn lblMsg.Text = "xml文件"+xmlFile+"不存在,请先点击创建!";rn return;rn rn// 方法一:通过ds 读取 xmlrn// DataSet ds = new DataSet();rn// ds.ReadXml(xmlFile);rn// if(ds.Tables.Count > 0)rn// rn// dgValue.DataSource = ds.Tables[0].DefaultView;rn// dgValue.DataBind();rn// rn //方法二rn DataTable dt = new DataTable();rn DataColumn dc = new DataColumn("uid");rn dt.Columns.Add(dc); rn dc = new DataColumn("name");rn dt.Columns.Add(dc);rn dc = new DataColumn("city");rn dt.Columns.Add(dc);rn dc = new DataColumn("email");rn dt.Columns.Add(dc);rn rn //将xml文件加载成xmlDocrn XmlDocument xmlDoc = new XmlDocument();rn xmlDoc.Load(xmlFile);rn //得到dbGust所有子节点rn XmlNodeList xnl=xmlDoc.SelectSingleNode("dbGuest").ChildNodes;rn rn for(int i = 0 ; i < xnl.Count;i++)rn rn //在每个子节点是遍历 xn 代表的是一个个Userrn string uid = xnl.Item(i).ChildNodes[0].Attributes["uid"].Value;rn string name = xnl.Item(i).ChildNodes[0].InnerText;rn string city = xnl.Item(i).ChildNodes[1].InnerText;rn string email = xnl.Item(i).ChildNodes[2].InnerText;rn DataRow dr = dt.NewRow();rn dr[0] = uid;rn dr[1] = name;rn dr[2] = city;rn dr[3] = email;rn dt.Rows.Add(dr); rn rn dgValue.DataSource = dt;rn dgValue.DataBind();rnrn rn rn private void btnDelete_Click(object sender, System.EventArgs e)rn rn string selectName = ddlNames.SelectedItem.Text;rn string selectID = ddlNames.SelectedValue;rn XmlDocument xmlDoc = new XmlDocument();rn xmlDoc.Load(xmlFile);rn XmlNodeList xnl=xmlDoc.SelectSingleNode("dbGuest").ChildNodes;rnrn rn for(int i = 0 ; i < xnl.Count ; i++)rn rn XmlElement xe=(XmlElement)xnl[i];//第i个dbGust子节点rn XmlNodeList node = xe.GetElementsByTagName("Name");rn if(node.Count>0)rn rn //判断为用户选择的rn if(node[0].InnerText==selectName && node[0].Attributes["uid"].Value == selectID)rn rn string m = node[0].InnerText;rn xmlDoc.SelectSingleNode("dbGuest").RemoveChild(node[0].ParentNode);//删除该User节点 rn //xe.RemoveAll();//删除该节点的全部内容 这二种有差别rn rn break;rn rn rn rn rn rn rn rn xmlDoc.Save(xmlFile);rn BindXML();rn GetXmlContent();rn BindUserNames();rn rn rn[/code] 论坛

没有更多推荐了,返回首页