问题:c#的数据库-DataRelation,现在一点头绪都没有,希望高手指点一下。
在编译运行程序提示异常:
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:");
//}
}
}
//}
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:");
//}
}
}
//}
...全文
相关推荐
自测试卷2.doc13.DataReader对象与DataSet对象有何区别? 14.线程在C#中使用什么类表示,你在什么情况下会考虑程序中使用线程?
**DataReader对象与DataSet对象的区别** - DataReader对象:它是.NET框架中用于从数据库检索数据的流式只读对象。
基于.NET的Web应用程序设计.doc
案例中描述的问题源于尝试删除一个与子表关联的父表行,但没有正确处理外键约束规则,导致异常。
ADO.NET中的多数据表操作之读取
DataSet可以理解为一个离线的数据存储,它可以包含从数据库中检索到的一组相关表。这些表可以通过DataRelation对象相互关联,形成一个关系网。
C#.Net的常见面试试题及答案
- **Forms身份验证**:自定义的身份验证方案,允许用户通过用户名和密码登录。以上是C#中的一些基本知识点和面试中可能涉及的问题解答,希望能够帮助到准备面试的朋友。
C#命名规范.pdf
希望本文能够帮助读者更好地理解和应用C#中的命名规范。


