错误1“DataRelation”是“命名空间”,但此处被当做“类型”来使用
#region using Directivesusing System;using System.Data; //Use ADO.NET namespaceusing System.Data.SqlClient; //Use SQL Server data provider namespaceusing System.Collections.Generic;using System.Linq;using System.Text;#endregionnamespace DataRelation{ class Program { static void Main(string[] args) { SqlConnection thisConnection = new SqlConnection("user id=pos;PWD=PO$$IBLE;Server=192.168.2.44;DataBase=gdbranch;TimeOut=30;"); SqlDataAdapter thisAdapter = new SqlDataAdapter( "SELECT CustomerID,Customername from Customers", thisConnection); SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter); DataSet thisDataSet = new DataSet(); SqlDataAdapter custAdapter=new SqlDataAdapter( "Select * from customers",thisConnection); SqlDataAdapter orderAdapter=new SqlDataAdapter( "SELECT * FROM Orders",thisConnection); custAdapter.Fill(thisDataSet,"Customers"); orderAdapter.Fill(thisDataSet,"Orders"); DataRelation custOrderRel = thisDataSet.Relations.Add("CustOrders", thisDataSet.Tables["Customers"].Columns["CustomerID"], thisDataSet.Tables["Orders"].Columns["CustomerID"]); foreach(DataRow custRow in thisDataSet.Tables["Customers"].Rows) { Console.WriteLine("Customer ID:"+custRow["CustomerID"]+ "Name:"+custRow["CompanyName"]); foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel)) { Console.WriteLine(" Order ID:"+orderRow["OrderID"]); } } thisConnection.Close(); Console.Write("Program finished,press Enter/Return to continue:"); Console.ReadLine(); } }}
DataRelation custOrderRel = thisDataSet.Relations.Add("CustOrders", thisDataSet.Tables["Customers"].Columns["CustomerID"], thisDataSet.Tables["Orders"].Columns["CustomerID"]);