表之间的关系 [英] relation between tables

查看:58
本文介绍了表之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个有2个表的数据集,我想在它们之间建立关系.我的2张桌子如下.

Hello,

I am having a dataset in which am having 2 tables,i want to build relation between them. my 2 tables are as listed below.

CREATE TABLE SERVICES
(VOY_SERVICES_SERVICE_ID INT PRIMARY KEY IDENTITY(1,1),
VOY_SERVICES_SERVICE_NAME VARCHAR(50))

CREATE TABLE SUB_SERVICES
(VOY_SUB_SERVICES_SERVICE_ID INT PRIMARY KEY IDENTITY(1,1),
VOY_SERVICES_SERVICE_ID INT FOREIGN KEY REFERENCES SERVICES (VOY_SERVICES_SERVICE_ID),
VOY_SUB_SERVICES_SERVICE_NAME VARCHAR(50))


我尝试过此代码,


i tried this code,

SqlConnection con = new SqlConnection(strconn);
        con.Open();
        SqlDataAdapter da1 = new SqlDataAdapter("select  *  from SERVICES", con);
        SqlDataAdapter da2 = new SqlDataAdapter("select  *  from SUB_SERVICES as sub,SERVICES as services where sub.VOY_SERVICES_SERVICE_ID = services.VOY_SERVICES_SERVICE_ID", con);
   DataSet ds2 = new DataSet();
        da1.Fill(ds2, "SERVICES");

        da2.Fill(ds2, " SUB_SERVICES");

ds.Relations.Add("servicessel", ds.Tables["SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"], ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]);
      ds.Relations[0].Nested = true;
        ds.Relations.Add(subservices, ds.Tables["SUB_SERVICES"].Columns["VOY_SUB_SERVICES_SERVICE_ID"], ds.Tables["SUB_SERVICES_CATEGORIES"].Columns["VOY_SUB_SERVICES_SERVICE_ID"]);
        ds.Relations[1].Nested = true;
 services_repeater.DataSource = ds2;
        services_repeater.DataBind();
con.Close();




in

ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]


其显示错误"Object reference not set to an instance of an object",请尽快帮助我解决此问题.


its showing an error "Object reference not set to an instance of an object", please help me to over come this problem asap.

推荐答案

对象引用未设置为对象的实例

当您尝试使用属性或调用对象的方法为null时,会发生此错误.更多详细信息:此处 [
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Based on what you say:
ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]


这意味着dsnullds.Tables["SUB_SERVICES"]nullds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"]null.如果有数据,请检查表名是否有错字或列名是否有错字.


This means either ds is null or ds.Tables["SUB_SERVICES"] is null or ds.Tables["SUB_SERVICES"].Columns["VOY_SERVICES_SERVICE_ID"] is null. If you have data, check for table name typo or column name typo.


您好shabadiveda,

您已经在 ds2 处添加了两个数据表,并在 ds 上建立了关系.

我认为您在输入错误的变量名或与其他变量名混用时犯了一个愚蠢的错误.
Hi shabadiveda,

You have added Both of your Datatable at ds2and Build relation On ds.

I think you have done a silly mistake of typing wrong variable name or mix up with other variable name.


这篇关于表之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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