1. 程式人生 > >三、DataSet數據集的操作

三、DataSet數據集的操作

create 一個數 llc 關閉 find text load tco test

1.DataSet的顯示

string connStr = "server=.;uid=sa;pwd=123;database=testDb";
using (SqlConnection conn = new SqlConnection(connStr))
{
     //實例化一個數據集
     DataSet ds = new DataSet();
     //數據適配器,用於DataSet和SQL數據庫的連接
     SqlDataAdapter sda = new SqlDataAdapter("select * from employee ", conn);
     //在DataSet中添加或刷新行
sda.Fill(ds); //將ds中的表顯示到dataGridView1控件中 dataGridView1.DataSource = ds.Tables[0]; }

2.DataSet的合並

string connStr = "server=.;uid=sa;pwd=123,;database=testDb";
using (SqlConnection conn = new SqlConnection(connStr))
{
     //實例化數據集ds1
     DataSet ds1 = new DataSet();
     SqlDataAdapter sda1 
= new SqlDataAdapter("select * from employee", conn); sda1.Fill(ds1); //實例化數據集ds2 DataSet ds2 = new DataSet(); SqlDataAdapter sda2 = new SqlDataAdapter("select * from testTable2", conn); //在DataSet中添加或刷新行 sda2.Fill(ds2); //將ds2與ds1合並,並處理不兼容的架構 ds1.Merge(ds2, true,MissingSchemaAction.AddWithKey);
this.dataGridView1.DataSource = ds1.Tables[0]; }

3.DataSet的復制

//數據集ds1
DataSet ds1;
private void Form1_Load(object sender, EventArgs e)
{
      string connStr = "server=.;uid=sa;pwd=123;database=TestDb";
      using (SqlConnection conn = new SqlConnection(connStr))
      {
           //SqlDataAdapter 數據一次性加載完成,連接就關閉了(數據量大的時候)
           //SqlDataReader 數據一行一行讀取,需要手動關閉連接(數據量小的時候)
           using (SqlDataAdapter sda = new SqlDataAdapter("select * from employee", conn))
           {
                ds1 = new DataSet();
                sda.Fill(ds1);
                dataGridView1.DataSource = ds1.Tables[0];
           }
      }
}
private void button1_Click(object sender, EventArgs e)
{
     //復制ds1的數據和結構到ds2
     DataSet ds2 = ds1.Copy();
     dataGridView2.DataSource = ds2.Tables[0];
}

4.DataSet的更新

DataSet ds1;
SqlDataAdapter sda;
private void button1_Click(object sender, EventArgs e)
{
      string connStr = "server=.;uid=sa;pwd=123;database=TestDb";
      //SqlConnection conn = new SqlConnection(connStr);
      SqlConnection conn = new SqlConnection(connStr);
      SqlCommand cmd = conn.CreateCommand();
      cmd.CommandText = "select * from employee";
      sda = new SqlDataAdapter();
      sda.SelectCommand = cmd;
      ds1 = new DataSet();
      sda.Fill(ds1, "oldDs");
      dataGridView1.DataSource = ds1.Tables[0];
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
      textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
      textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
      textBox3.Text = dataGridView1.SelectedCells[2].Value.ToString();
      textBox4.Text = dataGridView1.SelectedCells[3].Value.ToString();
      textBox5.Text = dataGridView1.SelectedCells[4].Value.ToString();
}

private void button2_Click(object sender, EventArgs e)
{
      DataTable dt = ds1.Tables["oldDs"];
      //將表結構加載到表中
      sda.FillSchema(dt, SchemaType.Mapped);
      DataRow dr = dt.Rows.Find(textBox1.Text);
      dr["name"] = textBox2.Text;
      dr["gender"] = textBox4.Text;
      dr["money"] = textBox3.Text;
      dr["salary"] = textBox5.Text;
      //用於對數據集DataSet進行更改操作,同時作用於關聯的數據庫
      SqlCommandBuilder scbd = new SqlCommandBuilder(sda);
      //更新DataTable中的數據
      sda.Update(dt);
      //sda.Update(ds1, "oldDs");
}

三、DataSet數據集的操作