EF5中 执行 sql语句使用Database.ExecuteSqlCommand 返回影响的行数 ; EF5执行sql查询语句 Database.SqlQuery 带返回值
2023-09-27 14:24:40 时间
一: 执行sql语句,返回受影响的行数
在mysql里面,如果没有影响,那么返回行数为 -1 ,sqlserver 里面 还没有测试过
using (var ctx = new MyDbContext()) { ctx.Database.ExecuteSqlCommand("UPDATE Person SET Name = 'Michael' WHERE PersonID = 1"); }
二 : Database.SqlQuery<T> EF5执行sql查询语句 Database.SqlQuery 带返回值
这个准确的说是 IEnumerable<T> SqlQuery<T>(string sql, params object[] parameters) ,注意返回值是 IEnumerable
这个是执行sql语句,返回你想要的类型的列表
dbMain.Database.SqlQuery<int>("select max(UserId) from tb_user_account").First();
或者假如你自己有个类别
public class PersonView { public int PersonID { get; set; } public string Name { get; set; } }
那么就可以直接返回这个 PersonView类
using (var ctx = new MyDbContext()) { var peopleViews = ctx.SqlQuery<PersonView>("SELECT PersonID, Name FROM Person").ToList(); }
直接返回你想要的数据. 例如这里是 List<PersonView> 列表
相关文章
- Mysql命令drop database:删除数据库
- PL/SQL package SYS.DBMS_BACKUP_RESTORE version 19.03.00.00 in TARGET database is not current
- recover database using backup controlfile/until cancel
- Oracle Database 11g : SQL 基础
- 倍福---database访问mysql数据库
- cadence原理图和PCB互联显示成功但是不能高亮和database
- 使用SolarWinds Database Performance Analyzer查找,分析和优化SQL Server性能
- SQL Server Database Encryption for GDPR Compliance with DbDefence
- PhpStorm中如何使用database工具
- Debain-4 安装 oracle 11.2.0.4 数据库database软件 (特别说明,完美)
- HTML5客户端数据存储机制Web Storage和Web SQL Database
- go的database/sql库中db.Exce()
- iOS中 DataBase SQL数据库 UI_高级
- implement of tree menu on database
- Database Comparer比较和同步,SQL 数据库对象和表记录
- 查看安卓sqlite数据库工具:Android-Debug-Database
- 已解决java.sql.SQLNonTransientConnectionException:Could not create connection to database server异常的正确解决
- Code First vs. Database First vs. Model First Approach
- 7.1 Connecting to a MySql database from Python
- [Windows Azure] Windows Azure Storage & SQL Database
- [Windows Azure] Managing SQL Database using SQL Server Management Studio
- [Windows Azure] Development Considerations in Windows Azure SQL Database
- Exception: The transaction log for database is full. To find out why space in the log cannot be reused, see the log_reuse_wait_desc.错误解决
- 异常message:There is no database named cloudera_manager_metastore_canary_test_db_hive_hivemetastore