sql server - Can someone explain how to set up a foreign key for a table in C#? -


i working on project creating tables within database in c# , cannot figure out how create foreign key.

here sql of trying create:

  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]     )   ) 

for example, trying set "references" part productkey in how references productkey column dimproduct table.

here code have creating productkey column in facttable table:

    //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); 

here code have dimproduct table trying reference:

//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(); 

i did find link: link1

it gives following example, cannot work:

//connect local, default instance of sql server.              server srv;             srv = new server();             //reference adventureworks2012 database.              database db;             db = srv.databases["adventureworks2012"];             //declare table object variable , reference employeedepartmenthistory table.              table tbea;             tbea = db.tables["employeedepartmenthistory", "humanresources"];             //define foreign key object variable supplying employeedepartmenthistory parent table , foreign key name in constructor.              foreignkey fk;             fk = new foreignkey(tbea, "test_foreignkey");             //add businessentityid foreign key column.              foreignkeycolumn fkc;             fkc = new foreignkeycolumn(fk, "businessentityid", "businessentityid");             fk.columns.add(fkc);             //set referenced table , schema.              fk.referencedtable = "employee";             fk.referencedtableschema = "humanresources";             //create foreign key on instance of sql server.              fk.create(); 

any or insight appreciated! thank you!

seems way code next nothing

in c# u have yor db connection db default or issue use mydatabase cmd

then fire off simple string equiv of

alter table orders add foreign key (persid) references persons(persid) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -