Establish a FOREIGN key constraint in the dataset

Source: Internet
Author: User

The program can be used directly to run through.

----------------------------------------------------
Testconstraint.aspx
----------------------------------------------------

<%@ Page language= "C #" debug= "true"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>

<script language= "C #" runat= "Server" >
Public DataSet DS;
void Page_Load (Object Sender,eventargs E)
{
if (! IsPostBack)
{
SqlConnection myconnection = new SqlConnection ("server= (local);d atabase=northwind; Trusted_connection=yes ");
SqlDataAdapter Mydataadapter1=new SqlDataAdapter ("select * from Suppliers", MyConnection);
SqlDataAdapter Mydataadapter2=new SqlDataAdapter ("SELECT * FROM Products", myconnection);

Ds=new DataSet ();
Mydataadapter1.fill (ds, "Suppliers");
Mydataadapter2.fill (ds, "products");

Mydatagrid.datasource=ds. tables["Suppliers"]. DefaultView;
Mydatagrid.databind ();
Mydatagrid2.datasource=ds. Tables["Products"]. DefaultView;
Mydatagrid2.databind ();
}
}

void Mydatagrid_delete (Object Sender,datagridcommandeventargs e)
{
String conn= "server= (local);d atabase=northwind; Trusted_connection=yes ";
String selectcommandtext1= "SELECT * from Suppliers";
String selectcommandtext2= "SELECT * FROM Products";

SqlDataAdapter Mydataadapter1=new SqlDataAdapter ();
Mydataadapter1.selectcommand=new SqlCommand ();
MYDATAADAPTER1.SELECTCOMMAND.COMMANDTEXT=SELECTCOMMANDTEXT1;
Mydataadapter1.selectcommand.connection=new SqlConnection (conn);

SqlDataAdapter Mydataadapter2=new SqlDataAdapter ();
Mydataadapter2.selectcommand=new SqlCommand ();
MYDATAADAPTER2.SELECTCOMMAND.COMMANDTEXT=SELECTCOMMANDTEXT2;
Mydataadapter2.selectcommand.connection=new SqlConnection (conn);

Ds=new DataSet ();
Mydataadapter1.fill (ds, "Suppliers");
Mydataadapter2.fill (ds, "products");

Createconstraint ();

int index= (int) E.item.itemindex;
Ds. tables["Suppliers"]. Rows[index]. Delete ();
Ds. tables["Suppliers"]. AcceptChanges ();

Mydataadapter1.update (ds, "Suppliers");

Mydatagrid.datasource=ds. tables["Suppliers"]. DefaultView;
Mydatagrid.databind ();

Mydatagrid2.datasource=ds. Tables["Products"]. DefaultView;
Mydatagrid2.databind ();
}

void Createconstraint ()
{
DataColumn Parentcolumn,childcolumn;
ForeignKeyConstraint Myforeignkeyconstraint;

ParentColumn = ds. tables["Suppliers"]. columns["SupplierID"];
Childcolumn = ds. Tables["Products"]. columns["SupplierID"];
Myforeignkeyconstraint = new ForeignKeyConstraint ("Supplierforeignkeyconstraint", ParentColumn, ChildColumn);

Myforeignkeyconstraint.deleterule = Rule.cascade;
Myforeignkeyconstraint.updaterule = Rule.cascade;
Myforeignkeyconstraint.acceptrejectrule = Acceptrejectrule.cascade;

Ds. Tables["Products"]. Constraints.add (Myforeignkeyconstraint);
Ds. EnforceConstraints =true;
}

</script>
<body>
<form runat= "Server" >
<asp:datagrid id= Mydatagrid "runat=" Server "
      width="
      Backcolor= "#ccccff"
      bordercolor= "BLACK"
      Showfooter= "false"
      cellpadding=3
      cellspacing = "0"
      font-name= "Verdana"
      font-size= "8pt"
      headerstyle-backcolor= "#aaaadd"
      Ondeletecommand = "Mydatagrid_delete"
      datakeyfield= "SupplierID"
    >

<Columns>
<asp:buttoncolumn text= "Delete Employee" commandname= "Delete"/>
</Columns>

</ASP:DataGrid>

<asp:datagrid id= "MyDataGrid2" runat= "Server"
Width= "800"
Backcolor= "#ccccff"
Bordercolor= "BLACK"
Showfooter= "false"
Cellpadding=3
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
/>

</form>
</body>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.