已有56人关注
请教一个数据合并的问题
发表在C#图书答疑 2016-07-28
是否精华
版块置顶:
    尊敬的专家,我在学习贵公司出版的《C#从入门到精通(第3版)》,学到第15章15.7.2“合并DataSet内容”时,参照教材内容写了一个程序,但出现一个疑问,现在将内容用一个WORD文档发给您,恳请专家帮助。
分享到:
精彩评论 2
zcmdyy
学分:0 LV1
2016-07-28
沙发
在C#的一个窗体中有一个button控件,写下列代码可以将一个DataSet得到的结果合并到另一个DataSet得到的结果中,并在dataGridView控件中显示。
private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select dqdm,dwdm, from Name1 ", conn);
            sda.Fill(ds);
            DataSet ds1 = new DataSet();
            SqlDataAdapter sda1 = new SqlDataAdapter("select dqdm,dwdm, from Name2", conn);
            sda1.Fill(ds1);
            ds1.Merge(ds, true, MissingSchemaAction.AddWithKey);
             dataGridView1.DataSource = ds1.Tables[0];
        }


但是如果窗体中有二个button控件,写下列代码就不可以将一个DataSet得到的结果合并到另一个DataSet得到的结果中
private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds1 = new DataSet();
            SqlDataAdapter sda1 = new SqlDataAdapter("select dqdm,dwdm, from Name2", conn);
            sda1.Fill(ds1);
        }
private void button2_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select dqdm,dwdm, from Name1 ", conn);
            sda.Fill(ds);
ds1.Merge(ds, true, MissingSchemaAction.AddWithKey);
             dataGridView1.DataSource = ds1.Tables[0];
        }
请教各位大侠这是为什么,要怎样才能实现这种情况的合并。

我写的全部代码在下面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace sjhb
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        public static DataSet ds1;
        MySqlConnection conn;

         private void Form1_Load(object sender, EventArgs e)
        {
            conn = new MySqlConnection("Server=127.0.0.1;User ID=root;Password='';Database=jf;PORT=3407;CharSet=utf8;");
         }

        private void button1_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter sda = new MySqlDataAdapter("select dqdm,dwdm,j_sl_ncxss ncxss from j2_1", conn);
            sda.Fill(ds);
            DataSet ds1 = new DataSet();
            MySqlDataAdapter sda1 = new MySqlDataAdapter("select dqdm,dwdm,j_sl_ncxss ncxss from j2_1", conn);
            MySqlCommandBuilder sbl = new MySqlCommandBuilder(sda1);
            sda1.Fill(ds1);
            ds1.Merge(ds, true, MissingSchemaAction.AddWithKey);
             dataGridView1.DataSource = ds1.Tables[0];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter sda = new MySqlDataAdapter("select dqdm,dwdm,j_sl_ncxss ncxss from j1_1", conn);
            sda.Fill(ds);
            ds1.Merge(ds, true, MissingSchemaAction.AddWithKey);
            dataGridView1.DataSource = ds1.Tables[0];
        }
    }
}
小科_mrkj
学分:43 LV2
2016-07-29
板凳
读者朋友:
    您好,把ds1和ds的声明放在这两个按钮click事件的外面。
首页上一页 1 下一页尾页 2 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照