SQLSugar 使用 SQL 语句执行 SQL 查询 - 代码示例和最佳实践
在 SQLSugar 中,可以使用以下代码执行 SQL 语句:
using (var db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = 'your connection string',
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
}))
{
//执行SQL语句,返回影响的行数
int count = db.Ado.ExecuteCommand('your sql command');
//执行SQL语句,返回单个值
object result = db.Ado.GetScalar('your sql command');
//执行SQL语句,返回DataTable
DataTable dataTable = db.Ado.GetDataTable('your sql command');
//执行SQL语句,返回List<T>
List<T> list = db.Ado.SqlQuery<T>('your sql command').ToList();
//使用参数化查询,返回List<T>
List<T> list = db.Ado.SqlQuery<T>('select * from table where id=@id', new { id = 1 }).ToList();
//执行SQL语句,返回dynamic类型的List
List<dynamic> list = db.Ado.SqlQueryDynamic('your sql command');
//使用参数化查询,返回dynamic类型的List
List<dynamic> list = db.Ado.SqlQueryDynamic('select * from table where id=@id', new { id = 1 });
}
注意: 在执行 SQL 语句时,应该使用参数化查询,以避免 SQL 注入攻击。
原文地址: https://www.cveoy.top/t/topic/ojC9 著作权归作者所有。请勿转载和采集!