關(guān)于dataset和datareader的區(qū)別,dataset這個(gè)問(wèn)題很多朋友還不知道,今天小六來(lái)為大家解答以上的問(wèn)題,現(xiàn)在讓我們一起來(lái)看看吧!
1、使用方法創(chuàng)建DataSet對(duì)象DataSet ds = new DataSet();DataSet ds = new DataSet("DataSetName");2、用數(shù)據(jù)集填充DataSet最常用的是DataAdapter對(duì)象的Fill()方法給他填充數(shù)據(jù)(1)DataSet ds = new DataSet();SqlDataAdapter adapt = new SqlDataAdapter(sqlcmd,con)adapt.Fill(ds,"mytest");(2)DataSet ds=new DataSet();DataTable dt=new DataTable("newTable");ds.Tables.Add(dt);(3)DataSet ds=new DataSet();DataTable dt=ds.Tables.Add("newTable");3、訪(fǎng)問(wèn)DataSet中的表、行和列 值(1): 訪(fǎng)問(wèn)每個(gè) DataTable按表名訪(fǎng)問(wèn):ds.Tables["mytest"] //指定DataTable對(duì)象mytest(即訪(fǎng)問(wèn)DataSet中名為mytest的DataTable)按索引(索引基于0的)訪(fǎng)問(wèn):ds.Tables[0] //指定DataSet中的第一個(gè)DataTable(2): 訪(fǎng)問(wèn)DataTable中的行ds.Tables["mytest"].Rows[n] //訪(fǎng)問(wèn)mytest表 的第n+1行(行的索引是從0開(kāi)始的)ds.Tables[i].Rows[n] //訪(fǎng)問(wèn)DataSet中的第i+1個(gè)DataTable 的第n+1列(列的索引是從0開(kāi)始的)(3): 訪(fǎng)問(wèn)DataTable中的某個(gè)元素ds.Tables["mytest"].Rows[n][m] //訪(fǎng)問(wèn)mytest表的第n+1行第m+1列的元素ds.Tables[i].Rows[n][m] //訪(fǎng)問(wèn)DataSet中的第i+1個(gè)DataTable 表的第n+1行第m+1列的元素ds.Tables["mytest"].Rows[n][name] //訪(fǎng)問(wèn)mytest表的第n+1行name列的元素ds.Tables[i].Rows[n][name] //訪(fǎng)問(wèn)DataSet中的第i+1個(gè)DataTable 表的第n+1行name列的元素(4): 取DataTable中的列名ds.Tables["mytest"].Columns[n] //取出mytest表的n+1列列名ds.Tables[i].Columns[n]4、實(shí)例using System;using System.Collections.Generic;using System.Text;using ***.Data.SqlClient;using ***.Data;namespace sqlconnection1{class Program{private void SQLConnectionF(string source, string select){//創(chuàng)建連接SqlConnection con = new SqlConnection(source);SqlDataAdapter adapt = new SqlDataAdapter(select,con);try{***.Open();Console.WriteLine("connection is successful!");}catch (Exception e){Console.WriteLine("connection error is :{0}", e.ToString());}//創(chuàng)建DataSetDataSet ds = new DataSet();//將數(shù)據(jù)添加到DataSet中adapt.Fill(ds,"mytest");//取出mytest表各列名Console.WriteLine("{0,-15} {1,-10} {2,-10}",ds.Tables["mytest"].Columns[0],ds.Tables["mytest"].Columns[1],ds.Tables["mytest"].Columns[2]);//輸出mytest表中第六行DataRow row1 = ds.Tables["mytest"].Rows[5];Console.WriteLine("{0,-15} {1,-10} {2,-10}",row1[0],row1[1],row1[2]);//輸出mytest表中第五行的第二列的值DataRow row2 = ds.Tables["mytest"].Rows[4];Console.WriteLine(" {0,-25} ", row2[1]);//下列兩種方法等效都等同于row2[1](即第五行的第二列的值)Console.WriteLine(" {0,-25} ", ds.Tables["mytest"].Rows[4][1]);Console.WriteLine(" {0,-25} ", ds.Tables["mytest"].Rows[4]["number"]);//輸出DataSet中的所有數(shù)據(jù)foreach (DataRow row in ds.Tables["mytest"].Rows){Console.WriteLine("{0,-15} {1,-10} {2,-10} {3}",row["name"] ,row["number"] , row["low"] , row["high"]);//取第三列的值Console.WriteLine("{0,-15} ", row[3]);}Console.ReadLine();con.Close();}static void Main(string[] args){string sou = "server=duanyf\SQLEXPRESS;" + "Initial Catalog=master;" + "UID = sa;" + "Password = dyf123";string sel = "SELECT name,number,low,high From dbo.spt_values";Program sqlcon = new Program();sqlcon.SQLConnectionF(sou, sel);}}}。
本文分享完畢,希望對(duì)大家有所幫助。
標(biāo)簽:
免責(zé)聲明:本文由用戶(hù)上傳,如有侵權(quán)請(qǐng)聯(lián)系刪除!