接着上次学习了ado.net数据提供对象部分,来学习用户操作部分,主要是关于DataSet的操作。
DateSet是ado.net核心,所有对数据的复杂操作都是使用它,DataSet包含一组DataTabel对象,他们表示所操作的数据表,每个DataTable都有两个子对象包含DataRow和DataColumn表示数据的行和列。
下面是一段代码用DataSet实现读取数据和更新数据
执行结果如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Data.SqlClient;
- using System.Data;
- using System.Text;
- namespace dataset
- {
- class Program
- {
- /// <summary>
- /// dataset学习
- /// </summary>
- /// 天行健 博客
- /// <param name="args"></param>
- static void Main(string[] args)
- {
- ///******************
- ///
- /// 天行健
- ///
- ///******************
- //读取表中的数据
- using (SqlConnection conn = new SqlConnection(@"data source=.\sqlexpress; database=Myoffice;user id=sa;password=123"))//连接数据库
- {
- string sql = "select Name,Age,Pay from Infor";
- SqlDataAdapter thisAdapter= new SqlDataAdapter(sql,conn);//建立sqlDataAdapter对象
- DataSet ds = new DataSet();//创建要填充数据的DataSet
- thisAdapter.Fill(ds,"Infor");//利用thisAdapter对象,在DataSet中创建Infor填充对象,并执行填充
- foreach (DataRow dr in ds.Tables["Infor"].Rows)//遍历DataSet对象
- {
- Console.WriteLine(dr["Name"]+"\t"+dr["Age"]+"\t"+dr["Pay"]);
- }
- ds.Clear();
- Console.WriteLine("Program finished!");
- //更新数据表
- Console.WriteLine("Now we will show update skills!");
- SqlCommandBuilder thisbuilder = new SqlCommandBuilder(thisAdapter);//创建CommBuilder对象,建立sql命令;负责生成更新数据的sql语句,而不要自己创建这个语句
- DataSet thisDataSet = new DataSet();//创建要填充的数据对象DataSet
- thisAdapter.Fill(thisDataSet,"table");//填充dataset对象
- Console.WriteLine("Name before change:{0}",thisDataSet.Tables["table"].Rows[3]["Name"]);
- thisDataSet.Tables["table"].Rows[3]["Name"] = "Hen";//更改数据
- thisAdapter.Update(ds,"Infor");//执行更新操作
- ///SqlDataAdapter.Update这个方法遍历DataTabe中的行,以找到需要对数据库做出的变动。
- ///Rows集合的每个DataRow对象都具有属性RowState,可以跟踪此行是否已删除、添加、修改,还是没有变动。反馈到数据库
- Console.WriteLine("Name after change:{0}",thisDataSet.Tables["table"].Rows[3]["Name"]);
- thisDataSet.Clear();
- Console.WriteLine("Updata finished!");
- Console.ReadLine();
- }
- Console.ReadKey();
- }
- }
- }
下面是执行结果
执行了DataSet的基本操作,下面是用DataSet执行对行的操作
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- using System.Data;
- namespace dataset2
- {
- class Program
- {
- static void Main(string[] args)
- {
- ///****************************
- ///
- /// 天行健
- ///
- ///*****************************
- using(SqlConnection conn=new SqlConnection(@"data source=.\sqlexpress;database=Myoffice;user id=sa;password=123"))
- {
- //添加行操作
- SqlDataAdapter thisAdapter = new SqlDataAdapter("Select * from Infor",conn);
- SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
- DataSet thisdataSet = new DataSet();
- thisAdapter.Fill(thisdataSet,"Table");
- Console.WriteLine("Rows after change:{0}", thisdataSet.Tables["Table"].Rows.Count);
- DataRow thisRow = thisdataSet.Tables["Table"].NewRow();//创建新行对象
- thisRow["Id"] = System.Guid.NewGuid();//获取guid算法值
- thisRow["Name"] = "Gerr";
- thisRow["Age"] = 27;
- thisRow["Pay"] = 1200;
- thisdataSet.Tables["Table"].Rows.Add(thisRow);
- Console.WriteLine("Rows after change:{0}",thisdataSet.Tables["Table"].Rows.Count);
- thisAdapter.Update(thisdataSet,"Table");
- ///dataset只是在内存中的数据,dataAdapter负责连接到磁盘上的数据库中,需要调用Update方法才能同步到数据库
- thisdataSet.Clear();
- Console.WriteLine("Finished!");
- //查找行操作
- SqlDataAdapter SelectAdapter = new SqlDataAdapter("Select * from Infor",conn);
- SqlCommandBuilder SelectBuilt = new SqlCommandBuilder(SelectAdapter);
- DataSet SelectDateSet = new DataSet();
- thisAdapter.Fill(SelectDateSet,"Table");
- //使用find之前,构建主键
- DataColumn[] keys = new DataColumn[1];
- keys[0] = SelectDateSet.Tables["Table"].Columns["Id"];
- SelectDateSet.Tables["Table"].PrimaryKey = keys;
- DataRow dr = SelectDateSet.Tables["Table"].Rows.Find("add4d8a6-0c99-4ad7-9628-2a6e548a6d73");
- if (dr != null)
- {
- Console.WriteLine("Find!");
- //删除行操作
- Console.WriteLine("Removing.....");
- dr.Delete();
- SelectAdapter.Update(SelectDateSet,"Table");
- Console.WriteLine("Removed!");
- }
- else
- {
- Console.WriteLine("Not Find");
- }
- SelectDateSet.Clear();
- Console.WriteLine("Finished!");
- Console.ReadLine();
- }
- }
- }
- }
执行结果:
一些基本讲解都在源码里,这些都是比较简单的东西,仅供菜鸟学习交流啊。呵呵。。。
下一次是关于ado.net多表的操作和关于读写xml内容。。。。。