我有一个包含两个表的数据集,我希望从第二个表中获得第一列的值,并将其初始化为int变量。
该列的名称是CONTACT_ID。
我试过这样做。
int Contract_id = Convert.ToInt32(dsDiscounts.Tables[1].Columns[0]);但它显示出一个错误:
无法将“System.Data.DataColumn”类型的对象强制转换为“System.IConverable”类型。
有人能帮帮我吗?
发布于 2014-05-14 07:27:38
dsDiscounts.Tables[1].Columns[0]返回列定义( DataColumn实例定义的数据类型、标题等)。当然,将列定义转换为整数失败。
您需要的是来自某一行表的单元格值(假设为第一行)。您应该使用Rows集合来访问表行。通过索引获得所需的DataRow后,可以访问行按指数、列名、列对象等中的单元格。例如,通过列名获取第一行的单元格值:
dsDiscounts.Tables[1].Rows[0]["CONTACT_ID"]发布于 2014-05-14 07:27:39
尝尝这个
int Contract_id = Convert.ToInt32(dsDiscounts.Tables[1].Rows[0]["CONTACT_ID"]);发布于 2020-10-13 06:35:49
public static T SafeGet<T>(this System.Data.DataSet dataset, string tableindex, int RowCount, string Nameofcolumn)
{
try
{
return dataset.Tables[tableindex].Rows[RowCount].IsNull(Nameofcolumn) == false ?
(T)Convert.ChangeType(dataset.Tables[tableindex].Rows[RowCount][Nameofcolumn], typeof(T))
: default(T);
}
catch (Exception ex)
{
throw ex;
}
}https://stackoverflow.com/questions/23648132
复制相似问题