无法将类型'System.Data.DataRelation'隐式转换为'DataRelation' [英] Cannot implicitly convert type 'System.Data.DataRelation' to 'DataRelation'

查看:53
本文介绍了无法将类型'System.Data.DataRelation'隐式转换为'DataRelation'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码出现错误.

Im getting error in following code.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class DataRelation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string connetionString = null;
        SqlConnection con;
        SqlDataAdapter da1;
        SqlDataAdapter da2;
        DataRelation relation;
        SqlCommand command = new SqlCommand();
        DataSet ds = new DataSet();
        connetionString = ConfigurationManager.ConnectionStrings["ConnectionStringDemo"].ConnectionString.ToString();
        con = new SqlConnection(connetionString);
        da1 = new SqlDataAdapter("select  top  (20)* from Production.Product", con);
        da2 = new SqlDataAdapter("select  top  (20) * from Sales.SalesOrderDetail", con);
        con.Open();
        da1.Fill(ds, "Production.Product");
        da2.Fill(ds, "Sales.SalesOrderDetail");
        con.Close();

        relation = ds.Relations.Add("CustOrders",
            ds.Tables["Production.Product"].Columns["ProductID"],
            ds.Tables["Sales.SalesOrderDetail"].Columns["ProductID"]);

        foreach (DataRow pRow in ds.Tables["Production.Product"].Rows)
        {
            Console.WriteLine(pRow["ProductID"]);
            foreach (DataRow cRow in pRow.GetChildRows(relation))
                Console.WriteLine("\t" + cRow["SalesOrderID"]);
        }
    }
}



请帮帮我..

[edit]已添加代码块-OriginalGriff [/edit]



pls help me..

[edit]Code block added - OriginalGriff[/edit]

推荐答案

System.Data命名空间已经包含一个名为DataRelation的类,并且该类是从DataSet返回的.添加方法调用.
您尝试将其存储在其中的变量被声明为DataRelation-这是您的方法所在的类名称.在这两个不相关的类型之间未定义任何转换.

更改类的名称(但不要更改relation的定义)或完全限定该定义:
The System.Data namespace already contains a class called DataRelation, and this is returned from a DataSet.Relations.Add method call.
The variable you have tried to store it in is declared as a DataRelation - which is the class name your method sits in. There is no conversion defined between the two unrelated types.

Either change the name of your class (but don''t change the definition of relation) or fully qualify the definition:
System.Data.DataRelation relation;

就我个人而言,如果我要使用System.Data,我不会将我的类称为冲突名称,只是为了避免混淆!

Personally, if I was going to use System.Data, I would not call my class a conflicting name, just to avoid confusion!


这篇关于无法将类型'System.Data.DataRelation'隐式转换为'DataRelation'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆