ADO.NET中有一个很好的功能,允许您在一次往返中将多个SQL语句发送到数据库,并接收所有语句的结果:
var command = new SqlCommand("SELECT count(*) FROM TableA; SELECT count(*) FROM TableB;", connection);
using(var reader = command.ExecuteReader())
{
reader.Read();
resultA = reader.GetInt32(0);
reader.NextResult();
reader.Read();
resultB = reader.GetInt32(0);
}
Run Code Online (Sandbox Code Playgroud)
Dapper.NET中是否有类似的功能?
异步控制器有不同的示例.其中一些在方法定义中使用CancellationToken:
public async Task<ActionResult> ShowItem(int id, CancellationToken cancellationToken)
{
await Database.GetItem(id, cancellationToken);
...
Run Code Online (Sandbox Code Playgroud)
但是其他示例甚至是VS2013的默认ASP.NET项目根本不使用CancellationToken而没有使用它:
public async Task<ActionResult> ShowItem(int id)
{
await Database.GetItem(id);
...
Run Code Online (Sandbox Code Playgroud)
目前尚不清楚,我们是否应该在控制器中使用CancellationToken(以及为什么).
c# asp.net-mvc async-await asp.net-web-api cancellation-token
我们使用IEnumerables从数据库中返回大量数据集:
public IEnumerable<Data> Read(...)
{
using(var connection = new SqlConnection(...))
{
// ...
while(reader.Read())
{
// ...
yield return item;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们想使用异步方法来做同样的事情.但是,async没有IEnumerables,因此我们必须将数据收集到列表中,直到加载整个数据集:
public async Task<List<Data>> ReadAsync(...)
{
var result = new List<Data>();
using(var connection = new SqlConnection(...))
{
// ...
while(await reader.ReadAsync().ConfigureAwait(false))
{
// ...
result.Add(item);
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
这将消耗服务器上的大量资源,因为所有数据必须在返回之前在列表中.IEnumerables处理大数据流的最佳且易于使用的异步替代方法是什么?我想避免在处理时将所有数据存储在内存中.
我想知道,我怎样才能跳过JQuery中的前N个元素.像这样的东西:
<div id="test">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
...
</div>
$('#test > div').skip(2)
Run Code Online (Sandbox Code Playgroud)
应该回来
<div>3</div>
<div>4</div>
...
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用:not(:first-child):not(:first-child + div)...选择器N次,但有更好的方法吗?
我正在使用Microsoft的一个名为DocumentDB的新数据库.现在我想通过ID删除文档,但我无法弄清楚,如何做到这一点.DocumentDB中的删除操作需要自我链接,它们与我自己的ID不同.
using (var client = new DocumentClient(EndPoint, AuthKey))
{
await client.DeleteDocumentAsync("**self-link here**");
}
Run Code Online (Sandbox Code Playgroud)
我可以执行一个额外的查询来查找自我链接,然后传递它,但这将需要两个操作而不是一个,这是我想要避免的.是否有更好的方法可以在不使用查询或存储过程的情况下按ID删除条目?
我目前正在使用ASP.NET MVC 4 CSS/JavaScript Optimizer.它适用于我自己的CSS/JavaScript,但我也想将它与插件一起使用.每个插件都有自己的文件夹:
~/Content/css // my own css, ok
~/Content/plugins/rateit
~/Content/plugins/chosen
~/Content/plugins/...
Run Code Online (Sandbox Code Playgroud)
我可以将这些文件添加到优化器:
var bundle = new Bundle("~/Content/opt", new CssMinify());
...
bundle.AddFile("~/Content/plugins/chosen/chosen.css", "*.css");
BundleTable.Bundles.Add(bundle);
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,优化后,css位于另一个文件夹中,浏览器无法再找到背景图像.是否有任何解决方案可以自动修改背景图像的css路径?
我可以将所有插件复制到一个文件夹中,但是有很多插件,这不是一个好主意.
这个css属性不适用于IE10:
font: bold 3rem/6rem Arial;
Run Code Online (Sandbox Code Playgroud)
但是,如果我将此属性拆分为单独的属性,它将起作用:
font-size: 3rem;
font-weight: bold;
line-height: 6rem;
font-family: Arial;
Run Code Online (Sandbox Code Playgroud)
我也可以使用PX而且它也有效:
font: bold 48px/96px Arial;
Run Code Online (Sandbox Code Playgroud)
您可以使用IE调试器在每个页面上尝试它.为什么这个属性不适用于IE,而是适用于所有其他浏览器?
我可以使用以下函数获取任何元素的背景颜色:
$('.example').css('background')
Run Code Online (Sandbox Code Playgroud)
但是,在我的情况下,鼠标正在移动此元素,并且由于CSS :hover伪类,我收到了修改后的颜色.
有没有办法收到原色?就像是
$('.example').cssWithoutHover('background')
Run Code Online (Sandbox Code Playgroud)
我使用此代码从Enterprise Library初始化RetryManager:
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
using Microsoft.Practices.TransientFaultHandling;
...
var manager = EnterpriseLibraryContainer.Current.GetInstance<RetryManager>();
return manager.GetDefaultSqlConnectionRetryPolicy();
Run Code Online (Sandbox Code Playgroud)
现在我将Enterprise Library NuGet包更新到最新版本,并且不再有EnterpriseLibraryContainer.
如何使用新的企业管理器初始化RetryManager?它必须是线程安全的,因为我的代码在Web服务器上执行.
如果在ASP.NET MVC中执行以下代码,您可以在调试窗口中看到await,即使ManagedThreadId发生更改,它也会正确恢复该文件的文化:
public async Task<ActionResult> Index()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
await SomeMethod();
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
return View();
}
private async Task SomeMethod()
{
await Task.Delay(100).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)
然后我将ConfigureAwait(false)从SomeMethod()移动到Index(),除此之外,它与上面的代码相同:
public async Task<ActionResult> Index()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("de-DE");
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
await SomeMethod().ConfigureAwait(false);
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
Debug.WriteLine(Thread.CurrentThread.CurrentUICulture);
return View();
}
private async Task SomeMethod()
{
await Task.Delay(100);
}
Run Code Online (Sandbox Code Playgroud)
现在它不会恢复我的文化,但始终将其设置为new CultureInfo("en-US").但我希望使用这两种方法,结果必须相同.它绝对不清楚,为什么会发生这种情况.
我的软件使用本地时间显示日期/时间,然后以UTC格式将其发送到服务器.在服务器端,我想在这个日期/时间添加月,年,周,日等.但是,问题是,如果我使用UTC日期/时间的这种方法,然后将其转换回本地时间,结果是否总是相同的,就好像我直接使用本地时间的方法一样?
这是C#中的一个例子:
// #1
var utc = DateTime.Now.ToUtcTime();
utc = utc.AddWeeks(2); // or AddDays, AddYears, AddMonths...
var localtime = utc.ToLocalTime();
// #2
var localtime = DateTime.Now;
localtime = localtime.AddWeeks(2); // or AddDays, AddYears, AddMonths...
Run Code Online (Sandbox Code Playgroud)
#1和#2的结果总是一样吗?或时区可以影响结果?
在我的计划中,我需要获取特定国家/地区的货币符号和国家/地区代码.
我可以从RegionInfo类中获取货币符号
new RegionInfo(abbreviation).CurrencySymbol
Run Code Online (Sandbox Code Playgroud)
但我找不到从.NET Framework获取国家/地区代码的方法.
c# ×7
.net ×3
async-await ×3
css ×3
asp.net-mvc ×2
azure ×2
jquery ×2
asp.net ×1
css3 ×1
dapper ×1
date ×1
html5 ×1
javascript ×1
sql ×1
sql-server ×1
time ×1
timezone ×1