使用DataAdapter操作数据库ASP实验报告.docx

上传人:b****8 文档编号:9596224 上传时间:2023-02-05 格式:DOCX 页数:16 大小:159.31KB
下载 相关 举报
使用DataAdapter操作数据库ASP实验报告.docx_第1页
第1页 / 共16页
使用DataAdapter操作数据库ASP实验报告.docx_第2页
第2页 / 共16页
使用DataAdapter操作数据库ASP实验报告.docx_第3页
第3页 / 共16页
使用DataAdapter操作数据库ASP实验报告.docx_第4页
第4页 / 共16页
使用DataAdapter操作数据库ASP实验报告.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

使用DataAdapter操作数据库ASP实验报告.docx

《使用DataAdapter操作数据库ASP实验报告.docx》由会员分享,可在线阅读,更多相关《使用DataAdapter操作数据库ASP实验报告.docx(16页珍藏版)》请在冰豆网上搜索。

使用DataAdapter操作数据库ASP实验报告.docx

使用DataAdapter操作数据库ASP实验报告

实训9:

使用DataAdapter操作数据库

实训目的:

1)通过上机练习进一步理解数据库连接对象Connection、命令对象Command、数据库适配器DataAdapter在数据库应用程序设计中的相互关系及各对象的创建和使用方法、步骤等。

2)通过本实验理解创建具有基本数据库管理功能(对数据记录的查询、添加、修改、删除)的应用程序和常用方法和技巧。

实训要求:

使用DataAdapter对象的Cimmand属性执行SQL语句,实现对数据库记录的查询、添加、修改和删除。

实训步骤:

1、为了实现程序的设计要求,将网站规划为4个网页,分别用于实现浏览(Default.aspx)、添加(Add.aspx)、修改(Update)和删除(Del.aspx)数据库记录的功能。

2、数据库浏览功能的实现

(1)设计Default.aspx页面

新建一个ASP.NET网站,将上次实验中新建的Acess数据库文件复制到站点下(App_Data)文件夹中。

切换到设计视图,向Default.aspx中添加3个链接按钮LinkButton1~3和一个GridView控件。

(2)设置对象属性3个链接按钮LinkButton的ID属性分别为LinkIns、LinkUpdate、LinkDel。

(3)编写程序代码

具体程序代码详见参考程序部分。

2、插入新纪录功能的实现

在“解决方案资源管理器”中添加一个名为Add.aspx的网页文件然后进行如下设置:

(1)设置Add.aspx页面

在Add.aspx的设计视图中,添加一个用于布局的HTML表格,向表格中添加必要的说明文字,添加6个文本框控件TextBox1~TextBox6,2个按钮控件Button1、Button2,1个单选钮组空间RadioButtonList1。

(2)设置对象属性

设置6个文本框的ID属性分别为TextNo、TextName、TextClass、TextMath、TextChinese、TextEnglish;设置2个按钮的ID属性分别为ButtonSubmit、ButtonBack;设置RadioButtonList1的ID属性为RadioSex,设置RepeatDirection属性为Horizon,向其中添加两个供选型“男”和“女”。

(3)编写程序代码

具体程序代码详见参考程序部分。

3、修改数据功能的实现

在“解决方案资源管理器”中添加一个名为Update.aspx的网页文件然后进行如下设置:

(1)设置Update.aspx页面

在Update.aspx的设计视图中,添加一个用于布局的HTML表格,向表格中添加必要的说明文字,添加1个下拉列表框控件DropDownList1,5个文本框控件TextBox1~TextBox5,2个按钮控件Button1、Button2,1个单选钮组空间RadioButtonList1。

(2)设置对象属性

设置DropDownList1的ID属性为DropNo,AutoPostBack属性为True;设置5个文本框的ID属性分别为TextNo、TextName、TextMath、TextChinese、TextEnglish;设置2个按钮的ID属性分别为ButtonSubmit、ButtonBack,设置它们的Text属性分别为“提交”和“返回”;设置RadioButtonList1的ID属性为RadioSex,设置RepeatDirection属性为Horizon,向其中添加两个供选型“男”和“女”。

(3)编写程序代码

具体程序代码详见参考程序部分。

4、删除记录功能的实现

在“解决方案资源管理器”中添加一个名为Del.aspx的网页文件然后进行如下设置:

(1)设置Del.aspx页面

在Del.aspx的设计视图中,添加一个用于布局的HTML表格,向表格中添加必要的说明文字,添加1个下拉列表框控件DropDownList1,1个用于显示当前记录的GridView控件、1个用户显示提示操作提示信息的标签控件Lable1、2个按钮控件Button1、Button2,1个用于向DropDownList1提供数据的AcessDataSource1。

(2)设置对象属性

设置DropDownList1的ID属性为DropNo,AutoPostBack属性为True;设置2个按钮的ID属性分别为ButtonSubmit、ButtonBack,设置它们的Text属性分别为“提交”和“返回”;设置标签控件的ID属性为LableMsg,设置其Text属性为空。

设置AcessDataSource1连接到Grade表的Sid字段,并将其作为下拉列表框控件DropNo的数据源。

(3)编写程序代码

具体程序代码详见参考程序部分。

5、在浏览器中查看网站中的网页信息

(1)页面加载完毕后得到的效果如图9-1所示。

此时显示的是所有同学的成绩信息。

图9-1页面加载后得到的效果图

(2)选择“插入记录”链接按钮之后显示的页面如图9-2所示。

插入记录成功后,系统弹出的提示信息如图9-3所示。

图9-2选择插入记录后显示的效果

图9-3记录插入成功

(3)返回首页,可以看到在显示全部学生成绩信息的表格中多了一个同学的信息,如图9-4所示,说明插入成功!

图9-4新纪录已经插入到数据库中

(4)在首页中选择“修改数据”链接按钮之后打开的页面效果如图9-5所示。

图9-5选择修改数据后显示的效果

(5)将“张三”同学的成绩全部改为60分,如图9-6所示,操作完成后系统弹出的提示信息如图9-7所示。

图9-6将“张三”同学的成绩都修改为60分

图9-7提示记录修改成功

(6)返回系统首页,查看张三同学的成绩,如图9-8所示,确实都变成了60分,说明修改操作成功!

图9-8修改后的张三的成绩

(7)在首页选中“删除记录”连接按钮后的效果如图9-9所示。

图9-9选择删除记录后出现的页面

(8)选中“张三”同学的学号后确定删除,弹出的提示信息如图9-10所示,然后返回首页重新查看,如图9-11所示,发现张三同学的信息已经没有了,说明删除成功。

图9-10删除张三同学成功后显示的提示信息

图9-11该数据库中已经没有张三的信息

实训总结:

本次实验主要练习了使用DataAdapter对象的Cimmand属性执行SQL语句来完成对数据库进行查找、修改、删除插入的操作。

让我了解了数据库连接对象Connection、命令对象Command、数据库适配器DataAdapter在数据库应用程序设计中的相互关系及各对象的创建和使用方法、步骤等。

同时我学到了创建具有基本数据库管理功能的应用程序和常用方法和技巧。

参考程序:

1、Default.aspx页面对应的代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data;

usingSystem.Data.OleDb;

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

this.Title="DataAdapter对象使用实例";

LinkIns.Text="插入记录";

LinkUpdata.Text="修改数据";

LinkDel.Text="删除记录";

OleDbConnectionconn=newOleDbConnection();

conn.ConnectionString="Provider=Microsoft.Jet.OleDb.4.0;"+"DataSource="+Server.MapPath("App_Data/Student.mdb");

stringStrSel="select*fromGradeorderbySid";

OleDbDataAdapterda=newOleDbDataAdapter(StrSel,conn);

DataTabledt=newDataTable();

da.Fill(dt);

GridView1.DataSource=dt;

GridView1.DataBind();

GridView1.Caption="浏览全部记录";

GridView1.Width=300;

conn.Close();

}

protectedvoidLinkIns_Click(objectsender,EventArgse)

{

Response.Redirect("add.aspx");

}

protectedvoidLinkUpdata_Click(objectsender,EventArgse)

{

Response.Redirect("update.aspx");

}

protectedvoidLinkDel_Click(objectsender,EventArgse)

{

Response.Redirect("del.aspx");

}

}

2、Add.aspx页面对应的代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data;

usingSystem.Data.OleDb;

publicpartialclassadd:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

this.Title="添加新记录";

TextNo.Focus();

}

protectedvoidButtonSubmit_Click(objectsender,EventArgse)

{

OleDbConnectionconn=newOleDbConnection();

conn.ConnectionString="Provider=Microsoft.Jet.OleDb.4.0;"+"DataSource="+Server.MapPath("App_Data/Student.mdb");

stringval="'"+TextNo.Text+"','"+TextName.Text+"','"+RadioSex.SelectedValue.ToString()+"','"+TextClass.Text+"',"+int.Parse(TextMath.Text)+","+int.Parse(TextChinese.Text)+","+int.Parse(TextEnglish.Text);

stringsqlins="insertintoGrade(Sid,Sname,Sgender,Class,Math,Chinese,English)values("+val+")";

OleDbCommandinscom=newOleDbCommand(sqlins,conn);

OleDbDataAdapterda=newOleDbDataAdapter();

conn.Open();

da.InsertCommand=inscom;

da.InsertCommand.ExecuteNonQuery();

conn.Close();

Response.Write("alert('新记录添加成功,请单击“返回”回到主页面!

');");

}

protectedvoidButtonBack_Click(objectsender,EventArgse)

{

Response.Redirect("Default.aspx");

}

}

3、Update.aspx页面对应的程序代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data;

usingSystem.Data.OleDb;

publicpartialclassupdate:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

this.Title="更新记录";

DropNo.AutoPostBack=true;

if(!

IsPostBack)

{

OleDbConnectionconn=newOleDbConnection();

conn.ConnectionString="Provider=Microsoft.Jet.OleDb.4.0;"+"DataSource="+Server.MapPath("App_Data/Student.mdb");

stringStrstr="select*fromGrade";

OleDbDataAdapterda=newOleDbDataAdapter(Strstr,conn);

DataTabledt=newDataTable();

da.Fill(dt);

DataRowmyrow=dt.Rows[0];

TextName.Text=myrow["Sname"].ToString();

if(myrow["Sgender"].ToString()=="男")

{

RadioSex.SelectedIndex=0;

}

else

{

RadioSex.SelectedIndex=1;

}

TextClass.Text=myrow["Class"].ToString();

TextMath.Text=myrow["Math"].ToString();

TextChinese.Text=myrow["Chinese"].ToString();

TextEnglish.Text=myrow["English"].ToString();

conn.Close();

}

}

protectedvoidButtonSubmit_Click(objectsender,EventArgse)

{

OleDbConnectionconn=newOleDbConnection();

conn.ConnectionString="Provider=Microsoft.Jet.OleDb.4.0;"+"DataSource="+Server.MapPath("App_Data/Student.mdb");

stringsqlstr="select*fromGradewhereSid='"+DropNo.Text+"'";

OleDbDataAdapterda=newOleDbDataAdapter(sqlstr,conn);

DataTabledt=newDataTable();

OleDbCommandBuilderbuilder=newOleDbCommandBuilder(da);

da.Fill(dt);

DataRowmyrow=dt.Rows[0];

myrow[1]=TextName.Text;

myrow[2]=RadioSex.SelectedValue.ToString();

myrow[3]=TextClass.Text;

myrow[4]=int.Parse(TextMath.Text);

myrow[5]=int.Parse(TextChinese.Text);

myrow[6]=int.Parse(TextEnglish.Text);

da.Update(dt);

Response.Write("alert('新记录添加更新成功,请单击”返回“回到主页面!

');");

conn.Close();

}

protectedvoidDropNo_SelectedIndexChanged(objectsender,EventArgse)

{

OleDbConnectionconn=newOleDbConnection();

conn.ConnectionString="Provider=Microsoft.Jet.OleDb.4.0;"+"DataSource="+Server.MapPath("App_Data/Student.mdb");

stringsqlstr="select*fromGradewhereSid='"+DropNo.Text+"'";

OleDbDataAdapterda=newOleDbDataAdapter(sqlstr,conn);

DataTabledt=newDataTable();

da.Fill(dt);

DataRowmyrow=dt.Rows[0];

TextName.Text=myrow["Sname"].ToString();

if(myrow["Sgender"].ToString()=="男")

{

RadioSex.SelectedIndex=0;

}

else

{

RadioSex.SelectedIndex=1;

}

TextClass.Text=myrow["Class"].ToString();

TextMath.Text=myrow["Math"].ToString();

TextChinese.Text=myrow["Chinese"].ToString();

TextEnglish.Text=myrow["English"].ToString();

conn.Close();

}

protectedvoidButtonBack_Click(objectsender,EventArgse)

{

Response.Redirect("Default.aspx");

}

}

4、Del.aspx页面对应的程序代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data;

usingSystem.Data.OleDb;

publicpartialclassdel:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

this.Title="删除记录";

LabelMsg.Text="单击”确定“将删除当前记录";

OleDbConnectionconn=newOleDbConnection();

conn.ConnectionString="Provider=Microsoft.Jet.OleDb.4.0;"+"DataSource="+Server.MapPath("App_Data/Student.mdb");

stringsqlstr="selecttop1*fromGrade";

OleDbDataAdapterda=newOleDbDataAdapter(sqlstr,conn);

DataTabledt=newDataTable();

da.Fill(dt);

GridView1.DataSource=dt;

GridView1.DataBind();

conn.Close();

}

}

protectedvoidButtonOK_Click(objectsender,EventArgse)

{

OleDbConnectionconn=newOleDbConnection();

conn.ConnectionString="Provider=Microsoft.Jet.OleDb.4.0;"+"DataSource="+Server.MapPath("App_Data/Student.mdb");

stringsqldel="deletefromGradewhereSid='"+DropNo.SelectedItem.Text

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1