DataSet与数据库相比,可能不能存储那么多数据,也可能不便于查询,但是可以脱机环境下工作。
ADO.Net断开连接模型使用的命名空间:System.Data.DataSet
//2010-9-7Dataset学习(通过代码构造一个DataSet)
DataSet ds = new DataSet();//实例化一个DataSet
DataTable dt = new DataTable("Student");//实例化一个DataTalbe
DataColumn c1 = new DataColumn();
c1.ColumnName = "学号";
c1.DataType = typeof(string);
DataColumn c2 = new DataColumn();
c2.ColumnName = "姓名";
c2.DataType = typeof(string);
//将两列添加到表里
dt.Columns.Add(c1);
dt.Columns.Add(c2);
//将表添加到DataSet
ds.Tables.Add(dt);
ds.Tables["Student"].Rows.Add("002", "张三");//弱类型,类型不明确,比较含糊
dt.Rows.Add("008", "李四");
DataSet ds = new DataSet();//实例化一个DataSet
DataTable dt = new DataTable("Student");//实例化一个DataTalbe
DataColumn c1 = new DataColumn();
c1.ColumnName = "学号";
c1.DataType = typeof(string);
DataColumn c2 = new DataColumn();
c2.ColumnName = "姓名";
c2.DataType = typeof(string);
//将两列添加到表里
dt.Columns.Add(c1);
dt.Columns.Add(c2);
//将表添加到DataSet
ds.Tables.Add(dt);
ds.Tables["Student"].Rows.Add("002", "张三");//弱类型,类型不明确,比较含糊
dt.Rows.Add("008", "李四");
强类型的DataSet
有XML架构(XSD)的DataSet,在本质上是个DataSet,但多了一个架构的定义
数据处理---强类型的DataSet
强类型的DataSet+TableAdapter+可视化设计 联机脱机都适用!
代码演示:
首先创建数据集:*.xsd
代码实例:
protected void Button1_Click(object sender, EventArgs e)
{
//添加数据
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
stu.Insert(this.TextBox1.Text, this.TextBox2.Text, int.Parse(this.TextBox3.Text),this.DropDownList1.SelectedValue);
this.Label1.Text = "操作成功!";
}
protected void Button2_Click(object sender, EventArgs e)
{
//删除数据
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
stu.Delete(this.TextBox1.Text);
this.Label1.Text = "操作成功!";
}
protected void Button3_Click(object sender, EventArgs e)
{
//模糊查询(自定义SQL)
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
this.GridView1.DataSource = stu.GetDataBy模糊查询(this.TextBox2.Text);
this.GridView1.DataBind();
this.Label1.Text = "操作成功!";
}
protected void Button4_Click(object sender, EventArgs e)
{
//显示所有数据
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
this.GridView1.DataSource = stu.GetData();
this.GridView1.DataBind();
this.Label1.Text = "操作成功!";
}
{
//添加数据
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
stu.Insert(this.TextBox1.Text, this.TextBox2.Text, int.Parse(this.TextBox3.Text),this.DropDownList1.SelectedValue);
this.Label1.Text = "操作成功!";
}
protected void Button2_Click(object sender, EventArgs e)
{
//删除数据
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
stu.Delete(this.TextBox1.Text);
this.Label1.Text = "操作成功!";
}
protected void Button3_Click(object sender, EventArgs e)
{
//模糊查询(自定义SQL)
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
this.GridView1.DataSource = stu.GetDataBy模糊查询(this.TextBox2.Text);
this.GridView1.DataBind();
this.Label1.Text = "操作成功!";
}
protected void Button4_Click(object sender, EventArgs e)
{
//显示所有数据
//定义TableAdapter
StudentDataSetTableAdapters.StudentTableAdapter stu = new StudentDataSetTableAdapters.StudentTableAdapter();
this.GridView1.DataSource = stu.GetData();
this.GridView1.DataBind();
this.Label1.Text = "操作成功!";
}
通过强类型的DataSet+视图
StudentDataSetTableAdapters.ViewStudentTableAdapter stu=new StudentDataSetTableAdapters.ViewStudentTableAdapter();
this.GridView1.DataSource=stu.GetData();
this.GridView1.DataBind();
this.GridView1.DataSource=stu.GetData();
this.GridView1.DataBind();
通过强类型的DataSet+存储过程
new StudentDataSetTableAdapters.QueriesTableAdapter().usp_insertuser(this.TextBox2.Text, this.TextBox3.Text, this.TextBox1.Text, this.DropDownList1.SelectedValue);