繁体   English   中英

有人可以解释如何在C#中为表设置外键吗?

[英]Can someone explain how to set up a foreign key for a table in C#?

我正在一个项目中,我正在用C#在数据库中创建表,但无法弄清楚如何创建外键。

这是我要创建的SQL:

  CREATE TABLE FactSalesOrders
  (ProductKey int NOT NULL REFERENCES DimProduct(ProductKey),
   CustomerKey int NOT NULL REFERENCES DimCustomer(CustomerKey),
   SalespersonKey int NOT NULL REFERENCES DimSalesperson(SalepersonKey),
   OrderDateKey int NOT NULL REFERENCES DimDate(DateKey),
   OrderNo int NOT NULL,
   ItemNo int Not NULL,
   Quantity int Not NULL,
   SalesAmount money NOT NULL,
   Cost money NOT NULL
        CONSTRAINT [PK_FactSalesOrders] PRIMARY KEY NONCLUSTERED
    (
        [ProductKey],[CustomerKey],[SalespersonKey],[OrderDateKey],[OrderNo],[ItemNo]
    )
  )

例如,我只是尝试为ProductKey设置“引用”部分,以了解它如何引用DimProduct表中的ProductKey列。

这是我在factTable表中创建ProductKey列的代码:

    //Creating Fact Table
    Table factTable = new Table(myDatabase, "Fact Table");

    //Column One: Product Key
    Column ProductKey = new Column(factTable, "ProductKey", DataType.Int);
    ProductKey.Nullable = false;
    factTable.Columns.Add(ProductKey);

这是我要引用的DimProduct表的代码:

//Creating DimProduct
            Table DimProduct = new Table(myDatabase, "DimProduct");

            //Column One: Product Key
            Column productKey = new Column(DimProduct, "ProductKey", DataType.Int);
            productKey.Nullable = false;
            DimProduct.Columns.Add(productKey);

            //Column Two: Product Alt Key
            Column productAltKey = new Column(DimProduct, "ProductAltKey", DataType.NVarChar(10));
            productAltKey.Nullable = false;
            DimProduct.Columns.Add(productAltKey);

            //Column Three: Product Name
            Column productName = new Column(DimProduct, "ProductName", DataType.NVarChar(50));
            productName.Nullable = true;
            DimProduct.Columns.Add(productName);

            //Column Four: Product Description
            Column productDescription = new Column(DimProduct, "ProductDescription", DataType.NVarChar(100));
            productDescription.Nullable = true;
            DimProduct.Columns.Add(productDescription);

            //Column Five: Product Catagory Name
            Column productCatagoryName = new Column(DimProduct, "ProductCatagoryName", DataType.NVarChar(50));
            productCatagoryName.Nullable = true;
            DimProduct.Columns.Add(productCatagoryName);

            //Primary Key
            Index primaryKeyIndex1 = new Index(DimProduct, "PK_DimProduct");
            primaryKeyIndex1.IndexKeyType = IndexKeyType.DriPrimaryKey;
            primaryKeyIndex1.IndexedColumns.Add(new IndexedColumn(primaryKeyIndex1, "productKey"));
            DimProduct.Indexes.Add(primaryKeyIndex1);

            DimProduct.Create();

我确实找到了此链接: LINK1

它给出了以下示例,但我无法使其正常工作:

//Connect to the local, default instance of SQL Server. 
            Server srv;
            srv = new Server();
            //Reference the AdventureWorks2012 database. 
            Database db;
            db = srv.Databases["AdventureWorks2012"];
            //Declare another Table object variable and reference the EmployeeDepartmentHistory table. 
            Table tbea;
            tbea = db.Tables["EmployeeDepartmentHistory", "HumanResources"];
            //Define a Foreign Key object variable by supplying the EmployeeDepartmentHistory as the parent table and the foreign key name in the constructor. 
            ForeignKey fk;
            fk = new ForeignKey(tbea, "test_foreignkey");
            //Add BusinessEntityID as the foreign key column. 
            ForeignKeyColumn fkc;
            fkc = new ForeignKeyColumn(fk, "BusinessEntityID", "BusinessEntityID");
            fk.Columns.Add(fkc);
            //Set the referenced table and schema. 
            fk.ReferencedTable = "Employee";
            fk.ReferencedTableSchema = "HumanResources";
            //Create the foreign key on the instance of SQL Server. 
            fk.Create();

任何帮助或见识将不胜感激! 谢谢!

似乎太多的代码几乎什么也没有

在C#中,您与数据库默认具有db连接或发出use mydatabase cmd

然后解雇一个简单的字符串等价物

ALTER TABLE Orders
ADD FOREIGN KEY (persId)
REFERENCES Persons(persId)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM