SQL SERVER-增加表聯絡,關聯查詢,增加主外來鍵約束
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();
/**********************建表*********************************************/
//DataTable products = new DataTable("Products");
//products.Columns.Add(new DataColumn("ProductID", typeof(int)));
//products.Columns.Add(new DataColumn("ProductName", typeof(string)));
//products.Columns.Add(new DataColumn("CategoryID", typeof(int)));
//ds.Tables.Add(products);
//DataTable catogries = new DataTable("catogries");
//catogries.Columns.Add(new DataColumn("CategoryID", typeof(int)));
//catogries.Columns.Add(new DataColumn("CategoryName", typeof(string)));
//ds.Tables.Add(catogries);
//ds.Relations.Add(new DataRelation("Products_Categoies_Rel",
// catogries.Columns["CategoryID"], products.Columns["CategoryID"]));
/**********************建表*********************************************/
/**********************填充資料*********************************************/
string source = "server=.;uid=sa;pwd=Server2012;database=D:\\DATABASE\\NORTHWND.MDF";
string sql = "select CategoryID,CategoryName from Categories";
SqlDataAdapter adapter = new SqlDataAdapter(sql, source);
adapter.Fill(ds, "Categories");
sql = "Select ProductName,CategoryID from Products";
adapter = new SqlDataAdapter(sql, source);
adapter.Fill(ds, "Products");
ds.Relations.Add(new DataRelation("Products_Categoies_Rel",
ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]));
/**********************填充資料*********************************************/
/**********************主從遍歷*********************************************/
//foreach (DataRow row in ds.Tables["Categories"].Rows)
//{
// Console.WriteLine("{0} Category contains:", row["CategoryName"]);
// foreach (DataRow child in row.GetChildRows("Products_Categoies_Rel")) //從父到子
// {
// Console.WriteLine("\t\t{0}", child["ProductName"]);
// }
//}
foreach (DataRow row in ds.Tables["Products"].Rows)
{
foreach (DataRow child in row.GetParentRows("Products_Categoies_Rel")) //從子到父
{
Console.WriteLine("Product {0} contained by Category {1}", row["ProductName"], child["CategoryName"]);
}
}
/**********************主從遍歷*********************************************/
/**********************設定主鍵*********************************************/
DataColumn[] pk = new DataColumn[1];
pk[0] = ds.Tables["Categories"].Columns["CategoryID"];
ds.Tables["Categories"].Constraints.Add(new UniqueConstraint("PK_Categories", pk[0]));
ds.Tables["Categories"].PrimaryKey = pk;
/**********************設定主鍵*********************************************/
/**********************設定外來鍵*********************************************/
ForeignKeyConstraint fk = new ForeignKeyConstraint(ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
fk.UpdateRule = Rule.Cascade;
fk.DeleteRule = Rule.SetNull;
ds.Tables["Products"].Constraints.Add(fk);
/**********************設定外來鍵*********************************************/
Console.Read();
}
}
}