zoukankan      html  css  js  c++  java
  • 【EF学习笔记03】----------使用原生Sql语句

    在EF中使用原生SQL,首先要创建上下文对象

    using (var db = new Entities())
    {
        //数据操作
    }

    新增

    string sql = "insert into UserInfo values('zhangsan','123456')";
        db.Database.ExecuteSqlCommand(sql);

    参数化新增

        sql = "insert into UserInfo values(@UserName,@UserPass)";
        var param = new SqlParameter[] 
        {
            new SqlParameter("@UserName","lisi"),
            new SqlParameter("@UserPass","123456")
        };
        db.Database.ExecuteSqlCommand(sql, param);

    删除

        sql = "delete from UserInfo where UserName='zhangsan'";
        db.Database.ExecuteSqlCommand(sql);

    参数化删除

        sql = "delete from UserInfo where UserName=@UserName";
        db.Database.ExecuteSqlCommand(sql, new SqlParameter("@UserName", "lisi"));

    修改

        sql = "update UserInfo set UserName='wangwu'";
        db.Database.ExecuteSqlCommand(sql);

    查询多条记录

        sql = "select * from UserInfo";
        var users = db.Database.SqlQuery<UserInfo>(sql);
        foreach (var user in users)
        {
            ObjectDumper.Write(user);//打印对象
        }

    总结:操作方式与ADO.NET类似,EF框架自动为我们封装了数据库链接和查询对象。

    完整案例:

    //1.创建上下文对象
                using (var db = new Entities())
                {
                    //
                    string sql = "insert into UserInfo values('zhangsan','123456')";
                    db.Database.ExecuteSqlCommand(sql);
    
                    //参数化新增
                    sql = "insert into UserInfo values(@UserName,@UserPass)";
                    var param = new SqlParameter[] 
                    {
                        new SqlParameter("@UserName","lisi"),
                        new SqlParameter("@UserPass","123456")
                    };
                    db.Database.ExecuteSqlCommand(sql, param);
    
                    //
                    sql = "delete from UserInfo where UserName='zhangsan'";
                    db.Database.ExecuteSqlCommand(sql);
    
                    //参数化删除
                    sql = "delete from UserInfo where UserName=@UserName";
                    db.Database.ExecuteSqlCommand(sql, new SqlParameter("@UserName", "lisi"));
    
                    //
                    sql = "update UserInfo set UserName='wangwu'";
                    db.Database.ExecuteSqlCommand(sql);
    
                    //原生sql查询
                    sql = "select * from UserInfo";
                    var users = db.Database.SqlQuery<UserInfo>(sql);
                    foreach (var user in users)
                    {
                        ObjectDumper.Write(user);//打印对象
                    }
                }
  • 相关阅读:
    Pycharm软件更换pip默认安装源为国内安装源
    电商网站名词item>SKU与SPU
    Linux通过端口号查看使用进程结束进程
    window系统下的pycharm对虚拟机中的Ubuntu系统操作MySQL数据库
    JAVA项目常用的异常处理情况总结
    公文流转系统(未完成)
    《程序员修炼之道》读后感(三)
    Java文件操作递归遍历文件目录
    Java Web初试连接数据库完成学生信息录入
    JavaJFrame窗口实现新课程添加
  • 原文地址:https://www.cnblogs.com/gosky/p/5751740.html
Copyright © 2011-2022 走看看