Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1791082
  • 博文数量: 787
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5015
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-22 15:17
文章分类

全部博文(787)

文章存档

2008年(787)

我的朋友

分类:

2008-09-25 16:10:39

[@more@]

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication3
{
///


/// Form1 的摘要说明。
///

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button3;
private SqlDataAdapter myDataAdapter1,myDataAdapter2;
private DataSet ds;
///
/// 必需的设计器变量。
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

///


/// 清理所有正在使用的资源。
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///


/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.splitter1 = new System.Windows.Forms.Splitter();
this.button2 = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(344, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 23);
this.button1.TabIndex = 0;
this.button1.Text = "显示用户库数据";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataGrid1
//
this.dataGrid1.AlternatingBackColor = System.Drawing.SystemColors.Window;
this.dataGrid1.BackColor = System.Drawing.SystemColors.Info;
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(464, 318);
this.dataGrid1.TabIndex = 1;
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 0);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(464, 3);
this.splitter1.TabIndex = 2;
this.splitter1.TabStop = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(200, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 23);
this.button2.TabIndex = 3;
this.button2.Text = "保存到消息库";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// panel1
//
this.panel1.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.panel1.Controls.Add(this.button3);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.button2);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(464, 21);
this.panel1.TabIndex = 4;
//
// button3
//
this.button3.Location = new System.Drawing.Point(64, 0);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 23);
this.button3.TabIndex = 4;
this.button3.Text = "保存到用户库";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(464, 318);
this.Controls.Add(this.panel1);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

///


/// 应用程序的主入口点。
///

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

//按钮事件
private void button1_Click(object sender, System.EventArgs e)
{
//定义数据库连接字符
string cnStr="Password=123456;Persist Security Info=False;User ID=sa;Initial Catalog=DBOA;Data Source=YORK";
//创建连接对象
SqlConnection cn=new SqlConnection(cnStr);
//定义SQL语句
string sqlStr="select * from t_cardcontent1";
//创建适配器对象
myDataAdapter1 = new SqlDataAdapter(sqlStr,cn);
//创建数据集对象
ds = new DataSet();
//将t_cardcontent1表填充到数据集
myDataAdapter1.Fill(ds,"t_cardcontent1");

//查询t_cardcontent7表的SQL语句
sqlStr="select * from t_cardcontent7";
//t_cardcontent7的适配器
myDataAdapter2 = new SqlDataAdapter(sqlStr,cn);
myDataAdapter2.Fill(ds,"t_cardcontent7");
//一次只能将一个SqlDataAdapter与一个SqlCommandBuilder对象互相关联
SqlCommandBuilder cb1=new SqlCommandBuilder(myDataAdapter1);
SqlCommandBuilder cb2=new SqlCommandBuilder(myDataAdapter2);

//构造数据关系对象,两个表之间的关系
DataRelation dr=new DataRelation("显示对应的消息库数据",ds.Tables["t_cardcontent1"].Columns["field1"],ds.Tables["t_cardcontent7"].Columns["owner"]);
//添加数据关系到数据集
ds.Relations.Add(dr);

//将数据集中的表t_cardcontent1绑定到DataGrid控件
dataGrid1.SetDataBinding(ds,"t_cardcontent1");

}

//更新不同的表需要不同的适配器
private void button2_Click(object sender, System.EventArgs e)
{
myDataAdapter2.Update(ds,"t_cardcontent7");//用已修改的数据集更新数据源
MessageBox.Show("成功保存到消息库!");
}

private void button3_Click(object sender, System.EventArgs e)
{
myDataAdapter1.Update(ds,"t_cardcontent1");
MessageBox.Show("成功保存到用户库!");
}
}
}



--------------------next---------------------

阅读(780) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~