asp 上机实验 数据访问
实验环境(实验设备)
Microsoft Visual Studio 2010,Microsoft SQL sever2008 数据库管理系统。 配置网络环境的计算机系统,安装有 IIS
实验原理及内容
1. 实验原理及内容 (1) 建立一个网站 Accessdatabase (2) 建立数据库 mydatabase (3) 建立职工表 Employ(ID ,Name,Sex,Age,Dataofwork,Photo) (4) 实现增,删,改,查页面及功能。 2.实验过程
<td colspan="2" style="text-align: center"> <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" Height="26px" /></td>
3
</tr> </table> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
增加 insert.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="insert.aspx.cs" Inherits="_Default" %>
<!DOCTYPE
html
PUBLIC
"-//W3C//DTD
XHTML
1.0
Transitional//EN"
{
string
sqlconnstr
=
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString
;
4
SqlConnection sqlconn = new SqlConnection(sqlconnstr);
</tr> <tr>
<td style="width: 100px; text-align: right"> 性别:</td>
<td style="width: 220px"> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Selected="True">男</asp:ListItem> <asp:ListItem>女</asp:ListItem> </asp:DropDownList>
</td> </tr> <tr>
<td style="width: 100px; text-align: right"> 年龄:</td>
<td style="width: 220px"> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
}
finally
5
{ sqlcommand = null; sqlconn.Close(); sqlconn = null;
}
} }
查询文件 select.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="select.aspx.cs" Inherits="select" %>
数学与计算机学院 上机报告
( 2012 / 2013 学年 第 2 学期 )
课程名称
网站开发
课程代码
6014789
上机时间
2013
年 5 月 15 日
指导单位
任课教师
李颖
学生姓名
杨玲
年级
学 号 312011080611403 专 业
成绩
2011 级 软件工程
实验名称
数据访问
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
}
protected void Button1_Click(object sender, EventArgs e)
//建立 Command 对象
SqlCommand sqlcommand = new SqlCommand();
sqlcommand.Connection = sqlconn;
//把 SQL 语句赋给 Command 对象
mandText
=
"insert
into
Employees(ID,NAME,SEX,AGE,Dateofwork,FilenameofPhoto)
values
(@ID,@NAME,@SEX,@AGE,@Dateofwork,@photo)";
sqlcommand.Parameters.AddWithValue("@ID", TextBox1.Text);
sqlcommand.Parameters.AddWithValue("@NAME", TextBox2.Text);
</tr> <tr>
<td style="width: 100px; text-align: right"> 姓名:</td>
<td style="width: 220px"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
6
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Employees]"> </asp:SqlDataSource>
<title></title> </head> <body>
<form id="form1" runat="server"> <div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Sex" HeaderText="Sex" SortExpression="Sex" /> <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" /> <asp:BoundField DataField="Dataofwork" HeaderText="Dataofwork" SortExpression="Dataofwork" /> <asp:BoundField DataField="Photo" HeaderText="Photo" SortExpression="Photo" /> </Columns>
</div> </form> </body> </html>