用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心.Net

在DataSet中建立外键约束

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-12 16:00:39

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

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

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论

栏目导航

本类热门阅览

相关文章

PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:7,968.75000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号