我的SQLite数据库中有一个可以为空的双列.
从数据库读取时(对于double类型的列),我想将空值转换为"double.NaN".
目前dapper将空值设置为0,这是我不想要的.
我有什么选择?
我的第一选择是选项1,但我需要帮助修改Dapper.
I am using dapper to add multiple new students in one db hit using this method:
db.ExecuteAsync(@"INSERT Student(Name,Age) values (@Name,@Age)",
students.Select(s => new { Name = s.Name, Age = s.Age })
);
Run Code Online (Sandbox Code Playgroud)
But the problem I don't have the new ids.
Can I make one db hit and still some how get the new ids ?
And if not, what is the most efficient way of performing such bulk insert ?
我正在使用Dapper的Query <>来搜索许多记录:
public class Product
{
public int Id {get; set}
public string Name {get; set}
public int CategoryId {get; set}
]
public IEnumerable<Product> GetProducts(int categoryId)
{
var connection = DataContext.Database.Connection;
var sql = "SELECT * FROM products WHERE category_id = @categoryId";
var result = connection.Query<Product>(sql, new { categoryId });
return result;
}
Run Code Online (Sandbox Code Playgroud)
查询本身返回请求的记录,但列表中的每个对象都有空字段.
那么如何将列映射到实体的属性?
我不想在sql语句中添加列别名.装饰实体的属性也不是一种选择,因为实体是由EF设计者生成的.
我有ASP.NET应用程序,我们使用Dapper库.产生错误的代码如下所示:
public bool CheckIfExists(IEnumerable<long> ticketGroups, long dateId, int userId)
{
bool bRetVal = false;
string sql = "if exists (select * from T_TicketGroupsToChangePrice where SubTypeId = @SubTypeId and DateId = @dateId and UserId = @userId)";
using (var conn = CreateSqlConnection())
try
{
int rows = conn.Execute(sql, ticketGroups.Select(g => new { SubTypeId = g, UserId = userId, dateId }));
if (rows > 0)
bRetVal = true;
}
catch (SqlException ex)
{
throw new Exception("Error", ex);
}
return bRetVal;
}
Run Code Online (Sandbox Code Playgroud)
当我运行应用程序时,它会抛出异常:')附近的语法不正确
如您所见,可以有更多具有相同日期和用户的票证(IEnumerable类型). …
我试图使用一个单独的Update语句来更新具有不同值的多个记录(我不是要尝试更新许多行以具有相同的值,这非常简单).这就是我现在正在尝试的:
using (var cn = GetOpenConnection()) {
// get items where we need to set calculated fields that will now be persisted in the DB
var items = cn.Query<MaintenanceItem>("select TOP 500 * from [Maintenance] where Tolerance IS NOT NULL");
foreach (var mi in maintItems)
{
// Set calculated fields on multiple recrods
logic.CalculateToleranceFields(mi, true);
}
var updateInput = items.Select(a => new {a.ToleranceMonths, a.ToleranceDays, a.ToleranceHours, a.ToleranceLandings, a.ToleranceCycles, a.ToleranceRIN }).ToList();
// THIS DOESN'T WORK - attempting to update multiple rows with different …Run Code Online (Sandbox Code Playgroud) 我执行此代码时收到NullReferenceException:
var insertTransaction = @"INSERT INTO [192.168.1.55].databaseName.dbo.tableName
(Date, Desc)
VALUES(GETDATE(),
@Description)
SELECT Scope_identity()";
var result = _sqlMapper.Query<int>(insertTransaction,
new
{
Description = "some description"
});
Run Code Online (Sandbox Code Playgroud)
其中_sqlMapper是一个实例 Dapper.SqlMapper
如果我删除SELECT Scope_identity()我不会得到例外.
异常堆栈跟踪说这里引发了异常:
在Dapper.SqlMapper.d__11`1.MoveNext()在d:\ Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:第1583行
SELECT Scope_identity()创建一个null对象,我该如何修复它?更新:Dapper版本= 1.40.0.0,运行时版本= v4.0.30319 DLL = C:\ src\packages\Dapper.1.40\lib \net45\Dapper.dll
更新:如果我在Management Studio中执行查询,则会插入行,但返回的scope_identity为null.
在我的c#应用程序中,我想将SQLite 与Dapper(on SqliteConnection)一起使用.
当前nuget System.Data.SQLite版本的问题是还加载了与EntityFramework相关的依赖项,但它们不是必需的.
using (IDbConnection dbConnection = openConnection)
{
string uQuery = "INSERT INTO User (Email, UserName, Password)"
+ " VALUES(@Email, @UserName, @Password)";
dbConnection.Open();
dbConnection.Execute(uQuery, User);
}
Run Code Online (Sandbox Code Playgroud)
我收到一个sql异常:关键字'User'附近的语法不正确.在db.Connection.Execute语句上.即使我省略了语句的User参数,我也会得到相同的错误.我插错了吗?
我是Dapper的初学者,对最佳做法有一些疑问。我的项目是Asp.net WebApi。
打开连接字符串
在此线程中,在Controller内以这种方式打开了与数据库的连接,但这是一个简单的项目,并不意味着要成为WebService:
static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString);
Run Code Online (Sandbox Code Playgroud)
但是我发现其他带有using声明的例子:
using (IDbConnection connection = new SqlConnection(stringConnection))
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
由于此项目是WebApi,因此using语句会更好,因为Dispose请求会更好吗?
上市数据
在上面的同一线程中,显示了如何基于static IDbConnection dbproperty 检索列表:
var res = (List<ShippDetails>)db.Query<ShippDetails>(query, new { id });
Run Code Online (Sandbox Code Playgroud)
还是会更好用.AsList()?
var res = connection.Query<ShippDetails>(query, new { id }).AsList();
Run Code Online (Sandbox Code Playgroud)
控制器的动作
对于我所有的动作,它看起来像:
[Route("FF")]
[HttpGet]
public async Task<HttpResponseMessage> get()
{
var response = new HttpResponseMessage();
int id = 1;
var res = (List<ShippDetails>)db.Query<ShippDetails>(query, new { id }); …Run Code Online (Sandbox Code Playgroud) 使用Dapper或Dapper.SimpleCRUD,如何从表中删除列表。就像是:
public static void DeleteList<T>(List<T> listToDelete)
{
using (var connection = OpenConnection())
{
connection.Delete<T>(listToDelete);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试时,我得到...
类型DataModel.MyTable的成员不能用作参数值
是传递WHERE子句的唯一选择吗?
dapper ×10
c# ×6
sql ×3
.net ×2
sql-server ×2
sqlite ×2
ado.net ×1
asp.net ×1
bulkinsert ×1
c#-4.0 ×1