您现在的位置: 军旅同心-旅游自驾-军旅文学 >> 读书赏析 >> 学习园地 >> 电脑网络 >> 技术文章 >> 正文
在DataSet中建立外键约束
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005-9-10 14:22:52

程序可以直接拿来使用,运行通过。

----------------------------------------------------
testconstraint.aspx
----------------------------------------------------

<%@ Page Language="c#" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<head>
<script language="c#" runat="server">
public DataSet ds;
void Page_Load(Object sender,EventArgs e)
{
    if(!IsPostBack)
    {
        SqlConnection myConnection = new SqlConnection("server=(local);database=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);database=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>
</head>
<body>
<form runat="server">
<ASP:DataGrid id="MyDataGrid" runat="server"
      Width="800"
      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>

<hr>
<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>
</html>


更多
免责声明:作品版权归所属媒体与作者所有!!本站刊载此文不代表同意其说法或描述,仅为提供更多信息。如果您认为我们侵犯了您的版权,请告知!本站立即删除。有异议请联系我们。
文章录入:烟灰缸    责任编辑:烟灰缸 
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 网站地图 | 版权申明 | 网站公告 | 管理登录 |