有人能解释一下如何在c#中为表设置一个外键吗?

本文关键字:设置 一个 能解释 一下 | 更新日期: 2023-09-27 17:51:15

我正在做一个项目,我在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设置"REFERENCES"部分,使其如何从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#中为表设置一个外键吗?

看起来代码太多了

在c#中,你有你的数据库连接db默认或发出使用mydatabase cmd

然后输出一个简单的字符串

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