c#关于DataRow的用法

来源:百度知道 编辑:UC知道 时间:2024/06/16 00:33:53
我的代码如下: DataRow drow;
DataSet dset = new DataSet() ;
drow = dset.Tables["idl"].Rows[0];
idl 是我的一个表名,我想用idl的第一行赋给drow 为什么会报错?“未将对象引用设置到对象的实例。” 谢谢大家。
第一句改成 DataRow drow = new DataRow();就会报另外一个错 “System.Data.DataRow.DataRow(System.Data.DataRowBuilder)”不可访问,因为它受保护级别限制 现在把DataSet dset = new DataSet();改成DataSet dset = new DataSet("idl") ; 还是会报错 ?“未将对象引用设置到对象的实例。” 现在我知道了是DataSet dset = new DataSet();这句话有问题,那么应该怎么改呢?

dset这个DataSet刚实例化,那来的表啊?
DataSet dset = new DataSet();这句话是没问题的!
你想这样drow = dset.Tables["idl"].Rows[0];
的话你必须建一个表
DataTable dt=new DataTable("idl");
dset.Merge(dt);
这样你就有表了,
但是还是没有行没有列,还要在表中自己加行加列!
要不给我发信息有什么问题直接帮你搞定

try
{
DataRow drow = null;
DataSet dset = new DataSet();
//先判断表是否存在
if(dset.Tables["idl"]==null)
{
throw new Exception("idl 表不存在");
}
if(dset.Tables["idl"].Rows.Count>0)
{
drow = dset.Tables["idl"].Rows[0];
}
else
{
throw new Exception("idl 表数据为空");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

dset.Tables
你的dset才创建 就有表了么?

第一句改为:DataRow drow = new DataRow();
没实例化

第一句改为:DataRow drow = new DataRow();

3.4楼正解.