我OdbcDataReader用来从我的数据库中获取结果,我想知道结果的行数.实现这一目标的最佳方法是什么?
public string[] GetHeaderAndColumnValues(string[] arrAllColumns, string[] arrExtColumns, string sRowDelimiterDisplayText, OdbcDataReader readerOdbc)
{
//Approach-1: Code to get row count but this is expensive approach.
using (DataTable dt = new DataTable())
{
dt.Load(readerOdbc);
int i = dt.Rows.Count;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我只想要行计数OdbcDataReader.
我有一个匿名类型的列表,我已经tagId, isTagSelected & tagName在其中.我想要的是根据某些条件添加第4个属性
码:
var tagDetails = tagIdList.Select((x, i) => new { tagId = x, isTagSelected = tagSelectionList[i], tagName = tagList[i] }).ToList();
Run Code Online (Sandbox Code Playgroud)
结果:
{tagId = 1, isTagSelected = true, tagName = "a"}
{tagId = 2, isTagSelected = false, tagName = "b"}
{tagId = 3, isTagSelected = true, tagName = "c"}
Run Code Online (Sandbox Code Playgroud)
我想,如果tagName是a再加入Size = "S"其他string.empty或null.我怎样才能做到这一点?我尝试了以下方法,即在所有这些方法中添加大小,然后从不需要的地方删除,但它无法正常工作.
码:
var tagDetails = tagIdList.Select((x, i) => new { tagId = x, isTagSelected = …Run Code Online (Sandbox Code Playgroud) 我有一个SQL表,其中列设置为null.现在我通过存储过程从代码传递null值,它抛出一个错误,即Procedure or function 'MyStoredProcedure' expects parameter '@Parameter', which was not supplied.
现在混淆部分对我来说,如果我添加空条件而不是传递DBNull.Value,它工作正常,但如果我传递直接参数可能null比它抛出错误.我想知道为什么?我在这里错过了什么吗?
注意:仅供参考,根据要求,传递参数可以为null.
码:
string connString = _dbContext.Database.Connection.ConnectionString;
using (SqlConnection connection = new SqlConnection(connString))
{
connection.Open();
SqlCommand cmd = new SqlCommand("MyStoredProcedure", connection);
cmd.CommandType = CommandType.StoredProcedure;
scheduledEndDateTime = !string.IsNullOrEmpty(bulkUpdateSchedule.EndDate) && !string.IsNullOrEmpty(bulkUpdateSchedule.EndTime)
? (DateTime?)
Convert.ToDateTime(bulkUpdateSchedule.EndDate + " " + bulkUpdateSchedule.EndTime)
: null;
//This Fails
cmd.Parameters.AddWithValue("@scheduledEndDateTime", scheduledEndDateTime);
//This Works
if (scheduledEndDateTime != null)
{
cmd.Parameters.AddWithValue("@scheduledEndDateTime", scheduledEndDateTime);
}
else
{
cmd.Parameters.AddWithValue("@scheduledEndDateTime", DBNull.Value);
}
cmd.ExecuteReader();
connection.Close();
}
Run Code Online (Sandbox Code Playgroud)
存储过程: …
我试图将日期转换为UTC格式,我可以获得正确的偏移量.我正在使用ToString("O")简单的DateTime.Now工作.
现在,当我将当前时间(EST)转换为CST(中央)或MST(山峰)时,我没有得到抵消.我在这里错过了什么?还有其他办法吗?
码:
var currentTimeToUtc = DateTime.Now.ToString("O");
// Output = "2018-12-27T12:31:21.9946661-05:00" --This is perfect.
var centralTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Central Standard Time");
var centralTimeToUtc = centralTime.ToString("O");
// Output = "2018-12-27T11:31:19.8046052"
// Expected Output = "2018-12-27T11:31:19.8046052-06:00"
var mountainTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Mountain Standard Time");
var mountainTimeToUtc = mountainTime.ToString("O");
// Output = "2018-12-27T10:31:25.2438418"
// Expected Output = "2018-12-27T10:31:25.2438418-07:00"
Run Code Online (Sandbox Code Playgroud) 我有一个简单的逻辑,我想INSERT使用 记录在表中AWS.DynamoDB.DocumentClient。下面是我正在使用的代码。在这里,我注意到.put正在更新记录,但不知道为什么。
在检查 CW 中的日志时,我没有看到任何错误,所以不确定我在这里错过了什么。
代码:
async addEvent(incomingModel: MyModel): Promise <MyModel> {
const dynamoModel: MyModel = {
fieldOne: incomingModel.fieldOne,
fieldTwo: incomingModel.fieldTwo,
"One#two#three": `${incomingModel.one}#${incomingModel.two}#${incomingModel.three}`,
fieldThree: incomingModel.fieldThree,
fieldFour: incomingModel.fieldFour,
};
var params = {
TableName: 'Table',
Item: dynamoModel,
};
return this.documentClient
.put(params)
.then((data) => {
this.logger.debug(
"Response received from Dynamo after adding an incomingModel",
data,
this.constructor.name,
);
return data as MyModel;
})
.catch((error) => {
const errorMessage = JSON.stringify(error);
this.logger.error(
`Error creating incomingModel with body of …Run Code Online (Sandbox Code Playgroud) amazon-web-services node.js amazon-dynamodb typescript documentclient
我正在使用AppDomain.CurrentDomain.BaseDirectory,我想向后退一步,但不知道如何?下面是示例
代码:
string path = AppDomain.CurrentDomain.BaseDirectory;
结果:
"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin\\Debug\\"
预期结果:
"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin"
我试图通过使用HttpClient但得到Not authorized错误来进行Web API调用.我在标题中传递密钥但仍然,它给了我这个错误.我可以看到我的钥匙fiddler trace.
如果我使用WebClient那么我会得到一个成功的回应.request两种方法都相同.
使用HttpClient:
#region HttpClient
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("apiKey", "MyKey");
var content = JsonConvert.SerializeObject(request);
var response = await client.PostAsJsonAsync("https://MyUrl", content);
if (response.IsSuccessStatusCode)
{
deliveryManagerQuoteResponse = await response.Content.ReadAsAsync<DeliveryManagerQuoteResponse>();
}
else
{
var reasonPhrase = response.ReasonPhrase;
if (reasonPhrase.ToUpper() == "NOT AUTHORIZED")
{
throw new KeyNotFoundException("Not authorized");
}
}
}
#endregion
Run Code Online (Sandbox Code Playgroud)
使用WebClient:
#region WebClient
// Create …Run Code Online (Sandbox Code Playgroud) 我有一个int,我想转换为特定格式的字符串.
例如,-1到-01:00
如果我有正数(即1到01:00),这很容易.我可以使用以下代码:
var time = 6;
var convertTime = "0" + time + ":00";
Run Code Online (Sandbox Code Playgroud)
输出:
06:00
Run Code Online (Sandbox Code Playgroud)
我不知道如何用负数实现相同.
var time = -6;
var convertTime = /* what to do here */ + time + ":00";
Run Code Online (Sandbox Code Playgroud)
期望的输出:
-06:00
Run Code Online (Sandbox Code Playgroud) 我有一个Generic Type Parameter用于动态使用它的类.现在,我正在使用if..else哪个适用于我的自定义类.我想知道我是否可以switch..case在这里使用.
如果数据类型是decimal或者int我可以TypeCode像这样使用.
switch (Type.GetTypeCode(typeof(T)))
{
case TypeCode.Int32:
break;
case TypeCode.Decimal:
break;
}
Run Code Online (Sandbox Code Playgroud)
但我有自己创建的自定义类,我想使用它们.下面的代码工作,并switch..case尽可能尝试使用.
我的代码:
void LogError<T>(T response, string orderId)
{
if (typeof(T) == typeof(QuoteResponse))
{
var quoteResponse = response as QuoteResponse;
//Do something
}
else if (typeof(T) == typeof(CommitResponse))
{
var commitResponse = response as CommitResponse;
//Do something
}
else if (typeof(T) == typeof(CancelResponse))
{
var cancelResponse = response as CancelResponse;
//Do something
} …Run Code Online (Sandbox Code Playgroud)