在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];
}
}
}