下面的代码和上面给出的差不多,我试验了一下,可以将从表的外键正确更新,不过存在一些显示的缺陷(没有深究),你试验一下,如果有问题再到这里讨论。
建立上面所示的两张表r1,r2,将代码copy过去后还要将sqlConnection1.ConnectionString 改为你机器上的数据库连接。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication7
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
DataSet ds;
SqlDataAdapter sda;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(376, 224);
this.dataGrid1.TabIndex = 0;
this.button1.Location = new System.Drawing.Point(160, 248);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 32);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//改为你的数据库连接
this.sqlConnection1.ConnectionString = ;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 294);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1,this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
ds=new DataSet();
sda=new SqlDataAdapter("select * from r1",this.sqlConnection1);
sda.FillSchema(ds,SchemaType.Mapped,"r1");
sda.Fill(ds,"r1");
sda=new SqlDataAdapter("select * from r2",this.sqlConnection1);
sda.FillSchema(ds,SchemaType.Mapped,"r2");
sda.Fill(ds,"r2");
ds.Relations.Add("abc",ds.Tables[0].Columns[0],ds.Tables[1].Columns[1]);
this.dataGrid1.SetDataBinding(ds,"r1");
}
private void button1_Click(object sender, System.EventArgs e)
{
SqlCommand insert_r1=new SqlCommand("Insert into r1 (name) values (@name);select id,name from r1 where (id=@@IDENTITY)",this.sqlConnection1);
insert_r1.Parameters.Add(new SqlParameter("@name",SqlDbType.Char,0,"name"));
sda.InsertCommand=insert_r1;
SqlCommand delete_r1=new SqlCommand("delete from r1 where id=@id",this.sqlConnection1);
delete_r1.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4,"id"));
sda.DeleteCommand=delete_r1;
SqlCommand update_r1=new SqlCommand("update r1 set name=@name where id=@id",this.sqlConnection1);
update_r1.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4,"id"));
update_r1.Parameters.Add(new SqlParameter("@name",SqlDbType.Char,0,"name"));
sda.UpdateCommand=update_r1;
sda.Update(ds,"r1");
SqlCommand insert_r2=new SqlCommand("Insert into r2 (parent_id,name) values (@parent_id,@name);select id,parent_id,name from r2 where (id=@@IDENTITY)",this.sqlConnection1);
insert_r2.Parameters.Add(new SqlParameter("@parent_id",SqlDbType.Int,0,"parent_id"));
insert_r2.Parameters.Add(new SqlParameter("@name",SqlDbType.Char,0,"name"));
sda.InsertCommand=insert_r2;
SqlCommand delete_r2=new SqlCommand("delete from r2 where id=@id",this.sqlConnection1);
delete_r2.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4,"id"));
sda.DeleteCommand=delete_r2;
SqlCommand update_r2=new SqlCommand("update r2 set parent_id=@parent_id where id=@id",this.sqlConnection1);
update_r2.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4,"id"));
update_r2.Parameters.Add(new SqlParameter("@parent_id",SqlDbType.Char,0,"parent_id"));
sda.UpdateCommand=update_r2;
sda.Update(ds,"r2");
}
}
}