连接到数据库后,我可以获取在我的所有列中返回的列的名称SqlDataReader吗?
为了避免我可以用Google搜索的所有标准答案,我将提供一个你可以随意攻击的例子.
C#和Java(和太多的人)有很多类型的一些"溢出"的行为,我不都不像(如type.MaxValue + type.SmallestValue == type.MinValue例如: int.MaxValue + 1 == int.MinValue).
但是,看到我的恶性,我会通过扩展这种行为来增加对这种伤害的一些侮辱,让我们说一个Overridden DateTime类型.(我知道DateTime密封在.NET中,但是为了这个例子,我使用的是一种与C#完全相同的伪语言,除了DateTime没有密封的事实).
被覆盖的Add方法:
/// <summary>
/// Increments this date with a timespan, but loops when
/// the maximum value for datetime is exceeded.
/// </summary>
/// <param name="ts">The timespan to (try to) add</param>
/// <returns>The Date, incremented with the given timespan.
/// If DateTime.MaxValue is exceeded, the sum wil 'overflow' and
/// continue from DateTime.MinValue.
/// </returns>
public DateTime override Add(TimeSpan …Run Code Online (Sandbox Code Playgroud) 我的问题是大多数开发人员更喜欢错误处理,异常或错误返回代码.请具体说明语言(或语言家族)以及为什么您喜欢其中一种语言.
我出于好奇而问这个问题.我个人更喜欢错误返回代码,因为它们不那么具有爆炸性,并且如果不想要,也不会强制用户代码支付异常性能损失.
更新:感谢所有答案!我必须说,虽然我不喜欢代码流与异常的不可预测性.关于返回代码(以及他们的哥哥句柄)的答案会给代码添加大量的噪音.
C#中的例外有多贵?只要堆叠不深,它们似乎并不是非常昂贵; 但我读过相互矛盾的报道.
有没有被反驳的确定性报告?
我有一个datareader从sql server数据库返回一个lsit记录.我在数据库中有一个名为"Additional"的字段.此字段为空或空的50%.
我正在尝试编写检查此字段是否为空的代码.这背后的逻辑是:如果字段"附加"包含文本,则显示信息,否则隐藏字段.
我试过了:
if (myReader["Additional"] != null)
{
ltlAdditional.Text = "contains data";
}
else
{
ltlAdditional.Text = "is null";
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给了我这个错误:
异常详细信息:System.IndexOutOfRangeException:Additional
任何帮助将不胜感激...
有没有办法查看基于IDataReader的对象中是否存在字段而不检查IndexOutOfRangeException?
本质上,我有一个方法,它采用一个基于IDataReader的对象,并创建一个强类型的记录列表.在一个实例中,一个数据读取器具有其他数据读取器不具有的字段.我真的不想重写所有提供此方法的查询,以便包含某些形式的此字段(如果我不需要).到目前为止,我能够弄清楚如何做到这一点的唯一方法是将1个唯一字段抛出到try/catch块中,如下所示.
try
{
tmp.OptionalField = reader["optionalfield"].ToString();
}
catch (IndexOutOfRangeException ex)
{
//do nothing
}
Run Code Online (Sandbox Code Playgroud)
有没有更简洁的方法将"可选字段"添加到其他查询或复制加载方法,因此1版本使用可选字段而另一个不使用?
我也在2.0框架中.
在方法参数中使用基类型有什么好处?
这是一个样本:
private void Foo(List<int> numbers) //R# laments: parameter can be IEnumerable.
{
foreach (var i in numbers) {
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)
这是另一个
public class Foo : ICloneable
{
public object Clone()
{
return MemberwiseClone();
}
public Foo CopyMe(Foo other) //R# laments: parameter can be ICloneable
{
return (Foo)other.Clone();
}
}
Run Code Online (Sandbox Code Playgroud)
...我们可以在哪里更改类型,但Foo在运行时会失败.
所以问题是:当R#建议参数可以是'X'时,我该怎么办?
PS.只是Whysharper的另一个解释- 一个与Resharper和StackOverflow相吻合的插件.人们说这很好,但缺乏很好的解释 - 希望我们能在一起做得更好;).
所以我使用IDataReader来水合一些业务对象,但我不知道在运行时究竟哪些字段将在读者中.任何不在阅读器中的字段都将在结果对象上保留为null.如何测试读者是否包含特定字段而不将其包装在try/catch中?
只有在IDataRecord中存在field_name时才有更好的方法从IDataRecord获取field_name值,目前我正在使用try {...} catch {...}块,但这是某种On Error接下来继续.一些替代品?
/// <summary>
/// Returns column value from IDataRecord only if field_name exists.
/// </summary>
public static Tresult ValueIfExists<Tresult>(this IDataRecord record, string field_name)
{
try { return record.Value<Tresult>(record.GetOrdinal(field_name)); }
catch { return default(Tresult); }
}
/// <summary>
/// Returns column value from IDataRecord accecing by index.
/// </summary>
public static Tresult Value<Tresult>(this IDataRecord record, int field_index)
{
return record.IsDBNull(field_index) ? default(Tresult) :
(Tresult)Convert.ChangeType(record[field_index], typeof(Tresult));
}
Run Code Online (Sandbox Code Playgroud)
我已经更改了我的ValueIfExists函数以反映您的想法,所以它看起来像这样:
public static Tresult ValueIfExists2<Tresult>(this …Run Code Online (Sandbox Code Playgroud) 我需要根据列值动态添加属性值.目前我正在创建对象如下:
while (rd.Read())
{
list.Add(new DeviceModelInfo()
{
ID = rd.GetGuid("ID"),
Manufacturer = new Manufacturer()
{
ID = rd.GetGuid("ManufacturerID"),
Name = rd.GetString("ManufacturerName"),
ManufacturerType (ManufacturerEnum)rd.GetInt32("ManufecturerType")
},
Model = rd.GetString("Model"),
IMEI = rd.GetString("IMEI")
});
}
Run Code Online (Sandbox Code Playgroud)
但是我需要检查IMEI列是否可用,DataReader但是当我使用if else然后它给出语法错误我想添加IMEI如果它可用,DataReader我该如何实现呢?
if (rd.GetName(i).Equals("IMEI", StringComparison.InvariantCultureIgnoreCase))
IMEI = rd.GetString("IMEI")
Run Code Online (Sandbox Code Playgroud)