我查看了堆栈上的几个线程,但无法找到答案。
我有一个打开SQL连接的应用程序使查询比较字符串,然后输出中继项目的消息或在第二个应用程序中创建项目。
App每次都被卡在相同的值上#J2; J2y7W0Q1Tn"(这个项目存在于DB中),当我暂停它时Visual Studio告诉我connAm.Open();将运行next ...我正在使用using语句,但需要我的变量在此范围之外可用。
我厌倦了尽可能地简化这一点,但仍然包括可能需要的东西。
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcumaticaRoundRobin.RoundRobin;
using System.Reflection;
using System.Globalization;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Threading;
#region GetExistingTransactions
try {
string CleanTAccessCode = Regex.Replace(TAccessCode, "(?:[^a-zA-Z0-9 ]|(?<=['\"])s)", "%", RegexOptions.None, TimeSpan.FromSeconds(1.5));
SqlConnection connAM = new SqlConnection();
connAM.ConnectionString = @"Data Source=myendpoint, 1433;Initial Catalog=DB;User Id=******;Password=***;Trusted_Connection=False;MultipleActiveResultSets=true;Connection Timeout=200000";
connAM.Close(); // Closing Connections that may still be open from last loop
connAM.Open(); //Open new connection
SqlCommand AccessCodeResult = new SqlCommand("SELECT [UsrAccessCode] FROM [AmSecureClose].[dbo].[PMTran] WHERE CompanyID = 3 AND UsrAccessCode LIKE '" + CleanTAccessCode + "'", connAM)
SqlDataReader AccessCodeResults = AccessCodeResult.ExecuteReader();
if (AccessCodeResults.HasRows) {
while (AccessCodeResults.Read()) {
string FlatUseResult = AccessCodeResults.IsDBNull(0) ? string.Empty : AccessCodeResults.GetString(0);
#endregion
if (FlatUseResult.Equals(TAccessCode, StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine(TAccessCode + " Exists Move to Next");
//connAM.Close();
}
else
{
//Jump into grid
context.CT303000Clear();
CT303000Content[] CT303000result2 = context.CT303000Submit(
new Command[] {
new Value { Value = ContractID, LinkedCommand = CT303000.Contract.ContractID },
CT303000.UnbilledTransactions.ServiceCommands.NewRow,
new Value { Value = /*DB Location ID*/TLocationIds, LinkedCommand = CT303000.UnbilledTransactions.LocationId },
//new Value { Value = /*DB Location*/TLocation, LinkedCommand = CT303000.UnbilledTransactions.Location },
new Value { Value = /*DB Deal Date*/TCreateDateS, LinkedCommand = CT303000.UnbilledTransactions.Date },
new Value { Value = /*DB Access Code*/TAccessCode, LinkedCommand = CT303000.UnbilledTransactions.AccessCode },
new Value { Value = /*DB Contract Code*/TContractCode, LinkedCommand = CT303000.UnbilledTransactions.ContractCode },
//new Value { Value = /*DB Contract Vin*/TContractVin, LinkedCommand = CT303000.UnbilledTransactions.ContractVin },
new Value { Value = /*DB Buyer ID*/TBuyerIdS, LinkedCommand = CT303000.UnbilledTransactions.BuyerId },
new Value { Value = /*DB First Name*/TBuyerFName, LinkedCommand = CT303000.UnbilledTransactions.BuyerFirstName },
new Value { Value = /*DB Last Name*/TBuyerLName, LinkedCommand = CT303000.UnbilledTransactions.BuyerLastName },
//new Value { Value = /*DB Showing Sttus ID*/TStatusIdS, LinkedCommand = CT303000.UnbilledTransactions.ShowingStatusId },
new Value { Value = /*DB Presentation Status*/TPresentationStatus, LinkedCommand = CT303000.UnbilledTransactions.PresentationStatus },
//new Value { Value = /*DB Machine Name*/TKioskName, LinkedCommand = CT303000.UnbilledTransactions.KioskName },
new Value { Value = "1", LinkedCommand = CT303000.UnbilledTransactions.Quantity},
new Value { Value = InventoryId, LinkedCommand = CT303000.UnbilledTransactions.InventoryID },
CT303000.Actions.Save
}
);
Console.WriteLine(TAccessCode + " Has Been Created Move to Next");
//connAM.Close();
}
}
}
}
//}
}
else
{
//No transactions for contract step to next Customer
}
}
#endregion
}
}
//connPro.Close();
} // End Timeout Exception
catch (Exception TimeoutException)
{
Console.WriteLine(TimeoutException.Message);
//if TimeoutException.Message
//Console.ReadLine();
continue;
}
答案 0 :(得分:0)
我需要使用包含connAM.Open()的“using”语句来构造此调用的开头。在第二个“SQLReader”之前使用查询和比较循环启动。
using (SqlConnection connAM = new SqlConnection()) { // Added this above the query enclosing the second using statement.
connAM.ConnectionString = @"Data Source=s*****, 1433;Initial Catalog=*****;User id=*****;Password=*****;Trusted_Connection=False;MultipleActiveResultSets=true;Connection Timeout=200000";
SqlCommand AccessCodeResult = new SqlCommand("SELECT [UsrAccessCode] FROM [AmSecureClose].[dbo].[PMTran] WHERE CompanyID = 3 AND UsrAccessCode LIKE '" + CleanTAccessCode + "'", connAM);
connAM.Open();
using (SqlDataReader AccessCodeResults = AccessCodeResult.ExecuteReader())
{