c#中的数据关系,实现浏览学生成绩

(1)建立一个Windows程序,可以上下翻页,查看学生的学号和姓名,同时显示该学生的所有成绩。

技术要点:

建立一个数据集ds,用数据适配器向da中填入两个表:studen和score,同时以studID为关联字段,在数据集中创建student表和score表之间的datarelation

(2)DataRelation主要的功能之一是允许您在DataSet中从一个datatable导航到另一个datatable。

(3)注意:

DataBindings属性是很多控件都有的属性,作用有2方面。一方面是用于与数据库的数据进行绑定,进行数据显示。另一方面用于与控件或类的对象进行数据绑定。这里主要关注后者。主要用法是将某个对象的某个属性与指定对象的指定属性进行关联.
Label、TextBox等都包含DataBindings属性,其类型为ControlBindingsCollection,是Binding类的集合。Binding类代表某对象属性值和某控件属性值之间的简单绑定。如可以将TextBox的Text属性值绑定到Label的Text属性值,这样,当TextBox中的文本被修改的时候,Label的文本也会及时进行修改,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 数据关系
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataSet ds, ds1;
        DataRelation studentScoreRel;//两个表之间的关联
        SqlDataAdapter sqlDataAdapter;


        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“xsglDataSet.score”中。您可以根据需要移动或删除它。
            this.scoreTableAdapter.Fill(this.xsglDataSet.score);
            //SqlConnection表示一个到sqlserver数据库的打开的连接
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = "server=localhost;uid=sa;pwd=root;database=xsgl";
            sqlDataAdapter = new SqlDataAdapter("select studID,studName from student",myConnection);
            ds = new DataSet();
            sqlDataAdapter.Fill(ds,"student");
            sqlDataAdapter = new SqlDataAdapter("select * from score",myConnection);
            sqlDataAdapter.Fill(ds,"score");
            //DataSet.Relations.Add("关联名称", 父关联主键字段, 子关联外来键字段,是否添加约束) 
            studentScoreRel = ds.Relations.Add("StudentScore",ds.Tables["student"].Columns["studID"],ds.Tables["score"].Columns["studID"]);
           //简单数据绑定指将一个控件绑定到单个数据元素的能力,事实上,控件上的任何属性都可以绑定到数据库中的字段。
            //Text是要绑定的控件的属性,这里是绑定textBox1的Text属性,ds是数据视图,第三个参数是绑定的字段名称。
            textBox1.DataBindings.Add(new Binding("Text",ds,"student.studID"));
            textBox2.DataBindings.Add(new Binding("Text", ds, "student.studName"));
            //
            this.BindingContext[ds, "student"].PositionChanged += new EventHandler(BindingManagerBase_PositionChanged);
            ds1 = new DataSet();
            ds1.Tables.Add(ds.Tables["score"].Clone());//将score表中的架构与约束进行复制并且给到ds1中、
            dataGridView1.DataSource = ds1;//DataSource表示dataGridView1中显示的数据的数据源是ds1
           dataGridView1.DataMember = "score";//DataMember显示数据的列表或表的名称。
            showScore();


        }

        private void BindingManagerBase_PositionChanged(object sender, EventArgs e)
        {
            showScore();
        }

        private void showScore()
        {
            ds1.Tables["score"].Rows.Clear();//清除score表中的数据
            //DataRow表示tables中的一行数据
            DataRow dr = ds.Tables["student"].Rows[this.BindingContext[ds, "student"].Position];
            //GetChildRows使用指定的DataRelation获得DataRow的子行
            foreach(DataRow dr1 in dr.GetChildRows(studentScoreRel)){//
                DataRow myDR = ds1.Tables["score"].NewRow();
                myDR["studID"]=dr1["studID"];
                myDR["courseID"] = dr1["courseID"];
                myDR["score"] = dr1["score"];
                ds1.Tables["score"].Rows.Add(myDR);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "student"].Position -= 1;//上一页
        }

        private void button2_Click(object sender, EventArgs e)
        {

            //下一页
            this.BindingContext[ds, "student"].Position += 1;

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值