使用Statement和ADO.NET对象(using Statement And ADO.NET Objects)
考虑以下方法......
private string[] GetExistingPOWSNumbers() { using ( var OleAdapter = new OleDbDataAdapter( "SELECT DISTINCT con_num FROM `Items` WHERE con_num LIKE 'POWS%'", ConfigurationManager.ConnectionStrings["MPOleConnectionString"].ConnectionString)) using (var POWSTable = new DataTable()) { OleAdapter.SelectCommand.Connection.Open(); OleAdapter.Fill(POWSTable); return POWSTable.AsEnumerable().Select(row => Convert.ToString(row["con_num"])).ToArray(); } }
是否所有ADO.NET对象都被及时处理? 我在整个项目中都使用这种方法,当在一次操作中有很多这样的调用时,我收到“Out of Memory”错误。
编辑:经过一些个人调查后,我发现,实际上,适配器的using语句也不会关闭提供的集合。 我做了以下改变。 现在我使用DataReader而不是填充DataTable。
private string[] GetExistingPOWSNumbers() { var Results = new List<string>(); using (var OleConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["MPOleConnectionString"].ConnectionString)) using ( var OleCommand = new OleDbCommand( "SELECT DISTINCT con_num FROM `Items` WHERE con_num LIKE 'POWS%'", OleConnection)) { OleConnection.Open(); using (var OleReader = OleCommand.ExecuteReader(CommandBehavior.CloseConnection)) { if (OleReader == null) return new string[0]; while (OleReader.Read()) { Results.Add(OleReader.GetString(0)); } } } return Results.ToArray(); }
Consider the following method...
private string[] GetExistingPOWSNumbers() { using ( var OleAdapter = new OleDbDataAdapter( "SELECT DISTINCT con_num FROM `Items` WHERE con_num LIKE 'POWS%'", ConfigurationManager.ConnectionStrings["MPOleConnectionString"].ConnectionString)) using (var POWSTable = new DataTable()) { OleAdapter.SelectCommand.Connection.Open(); OleAdapter.Fill(POWSTable); return POWSTable.AsEnumerable().Select(row => Convert.ToString(row["con_num"])).ToArray(); } }
Are all the ADO.NET objects promptly being disposed of? I am using this method throughout my projects and when there are A LOT of calls like these being made during a single action, I receive "Out of Memory" errors.
EDIT: After some personal investigation I discovered that, in fact, the using statement for the adapter DOES NOT also close the provided collection. I made the following change. Now I am using a DataReader instead of populating a DataTable.
private string[] GetExistingPOWSNumbers() { var Results = new List<string>(); using (var OleConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["MPOleConnectionString"].ConnectionString)) using ( var OleCommand = new OleDbCommand( "SELECT DISTINCT con_num FROM `Items` WHERE con_num LIKE 'POWS%'", OleConnection)) { OleConnection.Open(); using (var OleReader = OleCommand.ExecuteReader(CommandBehavior.CloseConnection)) { if (OleReader == null) return new string[0]; while (OleReader.Read()) { Results.Add(OleReader.GetString(0)); } } } return Results.ToArray(); }
原文:https://stackoverflow.com/questions/37122443
最满意答案
您仍然需要使用带有promise的回调函数:
doSomethingDN (var1, var3) .then(function(data) { // ^^^^^^^^^^^^^^^^ if (!data) { return doAnotherThingDN(var1,var2) } }); /* ^
你得到一个语法错误,因为你把函数体正确作为参数。
You still need to use a callback function with promises:
doSomethingDN (var1, var3) .then(function(data) { // ^^^^^^^^^^^^^^^^ if (!data) { return doAnotherThingDN(var1,var2) } }); /* ^
You were getting a syntax error because you put the function body right as the argument to
then
.
相关问答
更多-
$ q承诺与foreach($q promise with foreach)[2023-03-03]
看起来像this.UpdateListItem函数已经通过$promise对象返回$promise 。 这就是为什么你能够拥有.then (连锁承诺)功能的原因。 所以基本上你只需要推送返回的itemPromise对象而不是在promises数组中使用itemPromise.$promise 。 基本上当你做$promise ,它会创建一个[undefined, undefined, ...]数组,并在循环完成后立即解析。 改成 promises.push(itemPromise) 从 promises. ... -
没有“执行承诺”这样的事情。 Promise是异步操作结果的代理。 执行承诺与执行数字5相似。 你可以同步执行的是返回promise的函数。 这是reduce可以做的。 您可以使用allSettled原语而不是allSettled : var functions = [promiseReturningFn1, promiseReturningFn2, promiseReturningFn3]; functions.reduce((soFar, current) => { return soFar.the ...
-
是的,这有点奇怪,但有一点不同:将使用结果数组而不是结果本身调用responseFunc 。 这可能最好写成两者 promise.then(res => responseFunc([res])) 要么 promise.then(Array.of).then(responseFunc) Yes, this is a bit weird, but there is a difference: responseFunc will be called with an array of the result in ...
-
我认为db.run不是函数而是方法。 来自Q doc: 如果你正在使用方法而不是简单的函数,你可以很容易地遇到将方法传递给另一个函数的常见问题,比如Q.nfcall-从它的所有者“取消绑定”该方法。 为避免这种情况,您可以使用Function.prototype.bind或我们提供的一些不错的快捷方法: return Q.ninvoke(redisClient, "get", "user:1:id"); return Q.npost(redisClient, "get", ["user:1:id"]); ...
-
Q承诺 - 如何使用它?(Q promise - how to use it?)[2023-08-07]
您仍然需要使用带有promise的回调函数: doSomethingDN (var1, var3) .then(function(data) { // ^^^^^^^^^^^^^^^^ if (!data) { return doAnotherThingDN(var1,var2) } }); /* ^ 你得到一个语法错误,因为你把函数体正确作为参数。 You still need to use a callback function with promises: do ... -
对then的调用也会返回一个promise。 然后,您可以将其传递给您的数组而不是原始的承诺。 这样你的$q.all将在所有你的执行完毕后运行。 var promises = []; for(i=1, i<5, i++){ // singlePromise - this is now a new promise from the resulting then var singlePromise = SomeSevice.getData().then(function(data){ ...
-
我将简化3个承诺中的每一个(特别是1和3),以便this.locations,this.states和this.territories都在$ q.all中更新。然后 - 你不必,但我认为它使代码更具可读性 接下来,将所有3个promises分配给变量(下面的代码中的p1,p2,p3) 下一个问题是等待所有状态API调用完成 - 那里需要另一个$ q.all 总而言之,有了额外的ES6优点,你就得到了 getAllLocations() { //first prmise const p1 = ...
-
如何解决$ q的承诺?(how to resolve a $q for promise?)[2023-10-19]
当函数具有实际返回所需的所有数据时,您要做的是在promise上调用resolve函数 在返回promise的函数中,将“resolve”视为实际的return语句。 所以你的isLoggedInPromise函数也需要返回一个promise,这会让它变得多余。 任何需要用户的东西都必须等待它。 isLoggedInPromise().then(function(result){...etc...}, function(reason){//auth needed}) 如果你仍然需要isLoggedin函数, ... -
您可以返回firstResponse和methodB的结果: let deferred = Q.Promise(); this.methodA('somevalue') .then(firstResponse => { return Q.all([this.methodB(firstResponse.prop1), Q(firstResponse.prop1)]; }).then(secondResponse => { return Q.all([this.meth ...
-
有一点是你需要注入$rootScope才能使用它: app.service('LoadData', ['FeedService', 'EntryStateUrlService', '$q', '$rootScope', function(FeedService, EntryStateUrlService, $q, $rootScope) { //... } ]); 同样在Q库中有一个名为allSettled的方法,它允许您查看所有的promise结果,即使一个失败也是如此。 角度$ q没有这种 ...