- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
你好,
IDE Visual Studio 2010
.NET 3.5
平台WinForms
SO 问题“difference between getting value from DataRow”指的是。
我有一个包含列 [ID] INT IDENTITY(1, 1) NOT NULL PRIMARY KEY
的数据库表.当查询这个表并将值存储在局部变量中时,我得到一个无效的转换异常;示例代码:
string sQuery = @"
SELECT [ID], [Description]
FROM [Sources]
ORDER BY [Description] ";
using (DataTable dtSources = SQLHelper.Fetch(sQuery))
{
foreach (DataRow drSource in dtSources.Rows)
{
int iID = drSource.Field<int>("ID"); // InvalidCastException
string sDescrption = drSource.Field<string>("Description");
}
}
当逐步执行并在故障线路上执行“快速观察”时,我发现,通过将线路更改为 drSource.Field<object>("ID")
, 单元格值类型为 short
而不是 int
.为什么会发生这种情况,在表定义中,这显然是 int
?此外,short
应隐式转换为 int
自 short
更小,应该“适合”吧?
最佳答案
如果您的列是一个可为空的 int,但您试图分配给一个默认值为 0 的 int:
using (DataTable dtSources = SQLHelper.Fetch(sQuery))
{
foreach (DataRow drSource in dtSources.Rows)'
{
int iID = drSource.Field<int?>("ID") ?? 0;
string sDescrption = drSource.Field<string>("Description");
}
}
如果您的列是一个可为 null 的 int,并且您想分配给一个可为 null 的 int:
using (DataTable dtSources = SQLHelper.Fetch(sQuery))
{
foreach (DataRow drSource in dtSources.Rows)
{
int? iID = drSource.Field<int?>("ID");
string sDescrption = drSource.Field<string>("Description");
}
}
关于c# - DataRow.Field<T>(string Column) 抛出无效转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9885601/
我有一个扩展DataRow的类: import org.jdesktop.dataset.DataRow; public class MainDataRow extends DataRow { p
查看下面的代码块, DataTable _table = new DataTable(); //1) Why I need to Convert DataRow again into DataRow
我的代码: DataRow[] row = ByTotalTemplate.Select("TEMPLATE_ID=" + DisTemplateID); 一行包含TEMPLATE_ID、MIN_AM
使用这段代码: OracleDataTable dt = PlatypusSchedData.GetAvailablePlatypi(); OracleDataTable outputDt = new
假设我从多个线程访问一个DataTable。如果我想访问特定行,我怀疑我需要锁定该操作(我可能对此有误,但至少我知道这样我是安全的): // this is a strongly-typed tabl
我收到这个错误 "Error 8 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Coll
我有很多如下形式的代码: // assume that MyDataRow is a datarow from a typed dataset and has a property called So
在我的表中,有数据行注释,它存储为文本数据类型。 我有 Datarow idPersonalstamm,它存储为 int。 我的问题是,有时来自 Personalstamm 的数据会被复制到评论中。
我正在尝试将文本框中的新词添加到表格中: private void addAnswer_Click(object sender, EventArgs e) { // Get a new row
我的模块中有一个 cmdlet,它从数据库表中返回一组通用 DataRows,其名称由 -Name 指定。范围 # this returns a collection of datarows from
我在此代码行中得到一个空引用: **DataRow dr = tableSelectedItems.NewRow();** 我不知道为什么。谁能帮帮我吗?我的代码应该填写用户在数据列表中选择的书籍。
我尝试将值从自定义集合传递给 DataRow 类型变量,如下面的代码: GridHitInfo downHitInfo = null; DataRow row = gridView3.GetDataR
我正在尝试比较循环中的两个 DataRow。但是,以下 if 语句不会返回 true: if (dt1.Rows[0]["Name"] == dt2.Rows[b]["Name"]) { //
我在一个网站工作。我无法转换表格中行[""] 中的数值。我的代码是: public partial class bought : System.Web.UI.Page { protected
每当我在 MySql 中运行此查询时,它总是返回 3 行具有相同 ID,但记录不同的行,但在我的代码中,它只返回 1 行。 这是我的代码: public DataTable DBSelect(stri
我有以下问题。我将值作为 XML(如字符串)进行传递,并且不会为数字列传递默认值,例如 false 或 0。因此,我创建了一个字典,其中键是列名,值是传递的值。 这是我当前的代码 foreach (K
我正在尝试将 DataTable 中的所有 DateTime 值转换为字符串。这是我使用的方法: private static void ConvertDateTimesToStrings(DataT
我明白了: DataTable dtEntity = CreateDataTable(); drEntity = dtEntity.NewRow(); 然后我将数据添加到该行(或不添加)。一大堆代
我遇到了 DataRow 的问题,这让我很头疼。 使用 OleDbConnection 从 Excel 电子表格中读取数据行。 如果我尝试使用列名从 DataRow 中选择数据,即使那里有数据,它也会
我正在尝试编写一个单元测试来比较两个数组。我已经定义了单元测试: [DataTestMethod] [DataRow( new[] { "COM3", "COM1", "COM2" },
我是一名优秀的程序员,十分优秀!