异常:尝试在dataSet中的表之间创建关系时出现System.NullReferenceException

时间:2013-12-01 10:15:32

标签: c# .net database nullreferenceexception relation

使用C#,。net framework 4.5,VS 2012

尝试在数据集中的表之间创建简单的关系,但是得到System.NullReferenceException,正如我在MSDN上看到的那样,它的意思是occurs when you try to reference an object in your code that does not exist。但是,想想我创建了所有必需的对象。

我的代码如下:

    //create place for storing all tables from data base
    private DataSet myDS = new DataSet("AutoLot");

    //command builders for easy way access to tables
    private SqlCommandBuilder sqlCInventory;
    private SqlCommandBuilder sqlCOrders;
    private SqlCommandBuilder sqlCCustomers;

    //adapters for each table
    private SqlDataAdapter sqlAInventory;
    private SqlDataAdapter sqlAOrders;
    private SqlDataAdapter sqlACustomers;

    //connection string
    private string cnStr = string.Empty;

    public MainForm()
    {
        InitializeComponent();
        //get connection string from .config file
        cnStr = 
            ConfigurationManager.ConnectionStrings["AutoLotSqlProvider"].ConnectionString;
        //create adapters
        sqlACustomers = new SqlDataAdapter("Select * From Customers", cnStr);
        sqlAInventory = new SqlDataAdapter("Select * From Inventory", cnStr);
        sqlAOrders = new SqlDataAdapter("Select * From Orders", cnStr);

        //automatic generate commands 
        sqlCCustomers = new SqlCommandBuilder(sqlACustomers);
        sqlCInventory = new SqlCommandBuilder(sqlAInventory);
        sqlCOrders = new SqlCommandBuilder(sqlAOrders);

        //add table to data Set
        sqlAInventory.Fill(myDS);
        sqlAOrders.Fill(myDS);
        sqlACustomers.Fill(myDS);

        //create relationship between tables
        BuildTableRelationShip();

        //create DataSourse for datGrids on UI
        dataGridViewCustomer.DataSource = myDS.Tables["Inventory"];
        dataGridViewOrders.DataSource = myDS.Tables["Orders"];
        dataGridViewCustomer.DataSource = myDS.Tables["Customers"];
    }

在这里我得到了异常

    private void BuildTableRelationShip()
    {
        //create object of relationShips
        DataRelation dr = new DataRelation("CustomersOrders", //name of relation
            myDS.Tables["Customers"].Columns["CustID"], //main columns
            myDS.Tables["Orders"].Columns["OrderID"]); //related columns
        myDS.Relations.Add(dr);

        //second relation
        dr = new DataRelation("InventoryOrder",
            myDS.Tables["Inventory"].Columns["CarID"],
            myDS.Tables["Orders"].Columns["OrderID"]);
        //add relations to dataset
        myDS.Relations.Add(dr);
    }

为什么我得到这个Null引用异常?我想念的是什么?

修改

enter image description here

1 个答案:

答案 0 :(得分:2)

您应该致电fill个人DataTable而不是整个DataSet

sqlAInventory.Fill(myDS);
sqlAOrders.Fill(myDS);
sqlACustomers.Fill(myDS);

会变成

sqlAInventory.Fill(myDS, "Inventory");
sqlAOrders.Fill(myDS, "Orders");
sqlACustomers.Fill(myDS, "Customers");

如果表格不存在,此方法会自动向您的DataSet添加一个表格,如果存在则填充数据。 MSDN有关于此填充方法的更多信息。