问题:c#的数据库-DataRelation,现在一点头绪都没有,希望高手指点一下。

农叔叔 2005-01-24 03:16:04
在编译运行程序提示异常:
Customer ID:ALFKI
Order ID:10643
Order Date:1997-8-25 0:00:00

未处理的异常: System.NullReferenceException: 未将对象引用设置到对象的实例。
at usedb3.Main(String[] args) in f:\my work\c#\usedb3\usedb3\usedb3.cs:line 37
using System;
using System.Data;
using System.Data.SqlClient;

//namespace usedb3
//{
class usedb3
{
[STAThread]
static void Main(string[] args)
{
SqlConnection custConn=new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind;");
SqlDataAdapter custDA1=new SqlDataAdapter("select CustomerID,CompanyName from [Customers]",custConn);
SqlDataAdapter custDA2=new SqlDataAdapter("select OrderID,CustomerID,OrderDate from [Orders]",custConn);
SqlDataAdapter custDA3=new SqlDataAdapter("select OrderID,ProductID,Quantity from [Order Details]",custConn);
SqlDataAdapter custDA4=new SqlDataAdapter("select ProductID,ProductName from [Products]",custConn);

DataSet custDS=new DataSet();
custDA1.Fill(custDS,"Customers");
custDA2.Fill(custDS,"Orders");
custDA3.Fill(custDS,"OrderDetails");
custDA4.Fill(custDS,"Products");
//try
//{

foreach(DataRow custRow in custDS.Tables["Customers"].Rows)
{
Console.WriteLine("Customer ID:"+custRow["CustomerID"]);

DataRelation custOrderRel=custDS.Relations.Add("CustOrders",custDS.Tables["Customers"].Columns["CustomerID"],
custDS.Tables["Orders"].Columns["CustomerID"]);
foreach(DataRow orderRow in custRow.GetChildRows(custOrderRel))
{
Console.WriteLine(" Order ID:"+orderRow["OrderID"]);
Console.WriteLine("\tOrder Date:"+orderRow["OrderDate"]);

DataRelation orderDetailRel=new DataRelation("OrderDetails",custDS.Tables["Orders"].Columns["OrderID"],custDS.Tables["Order Details"].Columns["OrderID"]);
custDS.Relations.Add(orderDetailRel);
//是什么原因呢? 未处理的异常:System.NullReferenceException :未将对象引用设置到对象的实例

foreach(DataRow detailRow in orderRow.GetChildRows(orderDetailRel))
{
DataRelation orderProductRel=custDS.Relations.Add("OrderProducts",custDS.Tables["Products"].Columns["ProductID"],
custDS.Tables["Order Details"].Columns["ProductID"]);
Console.WriteLine("\t Product:"+detailRow.GetParentRow(orderProductRel)["ProductName"]);
Console.WriteLine("\t Quantity:"+detailRow["Quantity"]);
}
}
}
//}
//catch(System.NullReferenceException e)
//{
// Console.WriteLine("a0000000000System.NullReferenceException:");
//}
}
}
//}
...全文
156 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
aijing 2005-01-24
  • 打赏
  • 举报
回复
up
农叔叔 2005-01-24
  • 打赏
  • 举报
回复
还是一样的问题,在运行到一半的时候提示,异常。
zouzoujianjian 2005-01-24
  • 打赏
  • 举报
回复
DataRelation orderDetailRel=new DataRelation("OrderDetails",custDS.Tables["Orders"].Columns["OrderID"],custDS.Tables["Order Details"].Columns["OrderID"]);
custDS.Relations.Add(orderDetailRel);
//改成如下:(试试,是有区别的)
DataRelation orderDetailRel=custDS.Relations.Add("OrderDetails",custDS.Tables["Orders"].Columns["OrderID"],custDS.Tables["Order Details"].Columns["OrderID"]);


yiyi0518 2005-01-24
  • 打赏
  • 举报
回复
up~~~

学习学习~~~
作者:王华杰,李律松,孙一波 等编著 出版社:清华大学出版社 出版时间:2004年05月 第1章 数据库访问基础ADO.NET
1.1 ADO.NET概述
1.1.1 ADO.NET设计目标
1.1.2 ADO.NET结构
1.1.3 ADO.NET示例应用程序
1.2 .NET数据提供程序
1.2.1 使用ADO连接到数据源
1.2.2 Command命令
1.2.3 使用DataReader检索数据
1.2.4 使用DataAdapter
1.3 创建和使用DataSet
1.3.1 创建DataSet
1.3.2 向DataSet添加DataTable
1.3.3 添加表间关系DataRelation
1.3.4 导航表间关系
1.3.5 DataSet同数据源中的数据一起使用
1.3.6 合并DataSet内容
1.3.7 复制DataSet内容
1.3.8 使用DataSet事件
1.3.9 使用类型化的DataSet
1.4 DataSet和XML
1.4.1 DiffGram
1.4.2 从XML中加载DataSet
1.4.3 编写DataSet的XML数据形式
1.5 创建和使用DataTable
1.5.1 创建数据库
1.5.2 定义数据表的架构
1.5.3 在数据表中操作数据
1.6 创建和使用DataView
1.6.1 创建DataView
1.6.2 使用DataView对数据排序和筛选
1.6.3 使用DataView查看数据
1.6.4 使用DataView修改数据
1.6.5 使用DataView事件
1.6.6 使用DataViewManager设置默认表视图
1.7 小结
第2章 WinForm实现个人日程管理
2.1 案例简介
2.2 应用程序概述
2.3 方案设计
2.3.1 应用程序前端的设计
2.3.2 后端数据库表的设计
2.3.3 后端数据库表间逻辑
2.3.4 后端数据库存储过程的设计
2.4 方案实现
2.4.1 数据库访问公用类
2.4.2 日程浏览模块的实现
2.4.3 日程管理模块的实现
2.4.4 数据库操作
2.5 小结
第3章 学生信息报表系统的实现
3.1 案例简介
3.2 水晶报表简介
3.3 应用程序概述
3.4 方案设计
3.4.1 应用程序的结构
3.4.2 数据库设计
3.5 方案实现
3.5.1 添加数据集文件
3.5.2 使用Crystal报表专家生成报表
3.5.3 使用Crystal报表设计器
3.5.4 实现程序关键代码
3.5.5 运行结果
3.6 小结
第4章 电子相册的设计与实现
第5章 网上选课系统
第6章 ASP.NET实现论坛
第7章 ASP.NET实现网上商城
第8章 电子图书在线出版系统

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧