1. 程式人生 > >DataSet和DataTable區別,Connection、Command、DataReader、DataAdapter、DataSet

DataSet和DataTable區別,Connection、Command、DataReader、DataAdapter、DataSet

etc swd sin cursor tex add mman 性能 ado

DataSet和DataTable區別  
 da.Fill(ds, "alias_1");                            //使用DataAdapter的Fill方法(填充),調用SELECT命令,別名       
 dataGridView8.DataSource = ds.Tables[0];

 da.Fill(dt);       //直接填充給dt
 flex.DataSource = dt;
 
 ds.Tables[0]==dt
 DateSet是數據在內存中的緩存,是保存數據的數據結構,相當於一個虛擬的數據庫,他可以包含多個DateTable
 好像winform裏面綁定只能綁定dataTable
 簡單而言,dataset和datatable都是ADO的非連接層,dataset是一個位於內存中表示外部數據的對象,它是一個類型,該類型維護三個內部強類型集合,其中包括了datatablecollection,  dataset的tables屬性能夠訪問包含多個datatable的datatablecollection


 










        
private void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); conn.Open(); if (conn.State==ConnectionState.Open) { label1.Text = "數控庫已經連接並打開
"; } } private void button2_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); conn.Open(); if (conn.State == ConnectionState.Open) { label2.Text
= "數控庫已經連接並打開"; } conn.Close(); if (conn.State==ConnectionState.Closed) { label2.Text = "數控打開後又關閉了"; } } private void button3_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); conn.Open(); if (conn.State == ConnectionState.Open) { label3.Text = "數控庫已經連接並打開"; } conn.Dispose(); label3.Text = "conn.Dispose()"; } private void button4_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); conn.Open(); if (conn.State == ConnectionState.Open) { label4.Text = "數控庫已經連接並打開"; } SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select count(*) from khxjd"; cmd.CommandType = CommandType.Text; int i = Convert.ToInt32(cmd.ExecuteScalar()); label4.Text = "表中有" + i.ToString() + "條記錄!"; } private void button5_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); conn.Open(); if (conn.State == ConnectionState.Open) { label5.Text = "數控庫已經連接並打開"; } SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "update khxjd set qty=100+isnull(qty,0)"; cmd.CommandType = CommandType.Text; int i = Convert.ToInt32(cmd.ExecuteNonQuery()); label5.Text = "更新了表中" + i.ToString() + "條記錄!"; } //sqlDataReader,是向前的。 //針對於一個sqlConnection,創建多個sqlDataReader,要先關閉上一個sqlDataReader,才能使用下一個sqlDataReader. private void button6_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); conn.Open(); if (conn.State == ConnectionState.Open) { label6.Text = "數控庫已經連接並打開"; } SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd"; cmd.CommandType = CommandType.Text; SqlDataReader sdr= cmd.ExecuteReader(); while (sdr.Read()) { label6.Text = label6.Text + "," + sdr[1].ToString(); } label6.Text = label6.Text + "。讀取完成!"; } private void button7_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); conn.Open(); if (conn.State == ConnectionState.Open) { label7.Text = "數控庫已經連接並打開"; } SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd"; cmd.CommandType = CommandType.Text; SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.HasRows) { label7.Text = "有數據"; } else { label7.Text = "無數據"; } sdr.Close(); } //SqlDataAdapter:DataAdapter對象在DataSet與數據之間起橋梁作用 private void button8_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123"); //conn.Open(); //說明一下:使用 SqlDataAdapter時,只要SqlConnection有效就行,不需要 打開!!! if (conn.State == ConnectionState.Open) { label8.Text = "數控庫已經連接並打開"; } else { label8.Text = "數控庫還未打開"; //ok } //SqlCommand cmd = new SqlCommand(); //cmd.Connection = conn; //SqlCommand cmd = new SqlCommand( "select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd",conn); //SqlDataAdapter da = new SqlDataAdapter(); //da.SelectCommand = cmd; //DataSet ds = new DataSet(); // da.Fill(ds, "alias_1"); //使用DataAdapter的Fill方法(填充),調用SELECT命令,別名 //dataGridView8.DataSource = ds.Tables[0]; SqlDataAdapter da = new SqlDataAdapter("select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd", conn); //創建DataAdapter數據適配器實例 if (conn.State == ConnectionState.Open) { label8.Text = label8.Text+ "2.數控庫已經連接並打開"; } else { label8.Text = label8.Text + "2.數控庫還未打開"; //ok } DataSet ds = new DataSet(); //創建DataSet實例 da.Fill(ds, "alias_1"); //使用DataAdapter的Fill方法(填充),調用SELECT命令,別名 dataGridView8.DataSource = ds.Tables[0]; //label8.Text = "數控查詢完成"; if (conn.State == ConnectionState.Open) { label8.Text = label8.Text + "3.數控庫已經連接並打開"; } else { label8.Text = label8.Text + "3.數控庫還未打開"; //ok } //conn.Close(); //關閉數據庫 } // load table from db string sql = "select * from products"; string conn = GetConnectionString(); System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, conn); DataTable dt = new DataTable(); da.Fill(dt); //直接填充給dt // bind to grid _flex.Cols[0].Width = 20; _flex.ShowCursor = true; _flex.DataSource = dt; // add calculated field dt.Columns.Add("ValueInStock", typeof(decimal), "UnitPrice * UnitsInStock"); _flex.Cols["ValueInStock"].Format = "c"; // add unbound column Column col = _flex.Cols.Add(); col.Name = col.Caption = "Unbound";

DataSet和DataTable區別,Connection、Command、DataReader、DataAdapter、DataSet