动态网页程序设计Word文件下载.docx
《动态网页程序设计Word文件下载.docx》由会员分享,可在线阅读,更多相关《动态网页程序设计Word文件下载.docx(73页珍藏版)》请在冰点文库上搜索。
OnClientClick中写入:
returnconfirm("
真的要执行吗?
"
)
5.DataSet数据集及DataTable等对象
定义DataSet对象语法格式:
DataSet对象名=newDataSet()
说明在在DataSet可创建多表,体现其是内存中的数据库。
见示例DataAdapter_Reader中:
Datatable
上次讲的两个核心对象,讲到教材例7.18需要结合代码认真讲一下。
例7.18(教材207页)。
代码使用:
DataSet和DataAdapter不明晰,改造其代码。
见教材关于DataAdapter参数的叙述:
。
把:
SqlCommandMyCommand=newSqlCommand(SqlCommandString,MyConnection);
根据:
SqlDataAdapter对象名=newSqlDataAdapter(SQL语句,SqlConnection对象)
换为:
SqlDataAdapterMyCommand=newSqlDataAdapter(SqlCommandString,MyConnection);
带有两个参数,分别为SQL命令语句和Connection对象。
使用数据适配器DataAdapter后,就可填充数据集。
DataSetMyDataSet=newDataSet()
MyCommand.Fill(MyDataSet,"
authors"
);
注意到:
使用数据适配器DataAdapter和DataSet只是使用连接,不需要打开和关闭。
既不需要:
MyConnection.Open();
//打开连接
而在教材例7.17必须有:
在教材例7.17中,也可使用:
GridView1.DataSource=MyReader;
GridView1.DataBind();
见示例:
DataAdapter_Reader
例7.19使用对象DataView实现数据的排序与过滤。
也可在sql语句中使用排序和过滤。
例7.20使用DataAdapter对象的Update方法实现数据的更新。
补充:
练习使用页面登录,及登录上的一些问题。
结合例7-17到7-20无非是在sql语句中加入了条件选择语句where。
1、使用类登录。
建立连接类:
DB.cs(见文件)。
publicstaticSqlConnectioncreateCon()
{
returnnewSqlConnection("
Server=.;
uid=sa;
pwd=;
database=dataname"
}
建立初始登录页,用户名和密码
usingSystem.Data.SqlClient;
注意到usingSystem.Web.Security;
SqlConnectionmyConn=DB.createCon();
//对应上面类中的方法
分别用数据阅读器DataReader和数据适配器DataAdapter来实现登录
(1)使用数据阅读器
SqlCommandmyCommand=newSqlCommand("
select*fromDepartmentwhereZGBM='
+this.TextBox1.Text.ToString()+"
'
andpasswd='
+this.TextBox2.Text+"
myConn);
myConn.Open();
SqlDataReaderrecu=myCommand.ExecuteReader();
//或:
myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if(recu.Read())//判断密码是否正确
{
//后勤服务中心登陆
Session["
EmpName"
]=recu["
].ToString();
//名称
Response.Redirect("
hqfuzx/hqc_main.aspx"
//登录到页面
else
Response.Write("
<
script>
alert(\"
后勤处用户名或密码错误!
\"
/script>
this.lblMsg.Text="
用户名或密码错误!
;
}
myConn.Close();
说明:
若写成:
则可:
MyReader.Close();
则在关闭阅读器的同时,连数据库一起关闭。
(2)使用数据适配器
SqlDataAdaptermyDataAdapter=newSqlDataAdapter("
DataSetds=newDataSet();
myDataAdapter.Fill(ds,"
tab"
if(ds.Tables["
].Rows.Count>
0)
//Lable.Text=ds.Tables["
].Rows[0]["
字段名"
];
Response.Redirect("
this.lblMsg.Text="
==========================================================
2012-11-7日,3,4节讲到此。
在Web应用程序的开发过程中,Web安全是非常重要的,现存的很多网站也都存在一些非常严重的安全漏洞,
其中SQL注入是非常常见的漏洞,
1'
or'
='
1
解决方案1,对输入的密码值加密
passwordMD5.aspx
protectedvoidButton1_Click(objectsender,EventArgse)
//产生MD5格式的密码,并输出
this.Label_MD5.Text=System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(this.TextBox1.Text,"
MD5"
如加密1为:
C4CA4238A0B923820DCC509A6F75849B
把此密码存入数据库表中。
登录时,同样对输入到password文本框中字符加密,和数据库中的加密值比较,若一样,则通过。
若引入System.Web.Security名称空间,则句子中可省略。
stringupwd=
FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassWord.Text,"
把登录语句中的textbox2.Text换为upwd。
可解决注入漏洞。
解决方案2
如果将查询语句进行参数化查询,可以减少SQL注入漏洞的概率,
参数化查询示例代码如下所示。
//使用参数,(注意必须使用SqlCommand,不能用SqlDataAdapter,使用SqlDataAdapter就不能定义参数了)
select*fromuser_passwordwhereuser_name='
+this.Login1.UserName.ToString()+"
andpassword=@passwordx"
myConn.Open();
//添加并定义参数名为:
@passwordx
myCommand.Parameters.Add(newSqlParameter("
@passwordx"
SqlDbType.VarChar));
//给参数取值
myCommand.Parameters["
].Value=@Login1.Password;
//注意此处
SqlDataReaderrecu=myCommand.ExecuteReader();
//登陆
D_men"
Depart_men"
//获得数据库表信息
Default2.aspx"
Response.Write("
回过头,再讲第4章:
(87页)
Repert控件和Datalist控件,GridView控件。
4.2.4Repeater控件
Repeater用于对绑定数据源中的数据进行遍历显示,每条数据一什么格式显示是由Repeater的<
ItemTemplate>
来决定的,模版会多次显示
姓名:
%#Eval("
Name"
)%>
b>
br>
年龄:
Age"
/b>
表示在这个位置显示当前行Name属性,注意调用Eval、Bind这些数据绑定方法是要用#.
因为Eval就是将属性显示到指定位置,因此也可以显示到文本框中
inputtype=”text”value=’%#Eval("
’/>
注意不要写成value=”%#Eval("
”(双引号),因为<
%%>
中是C#代码,“是字符,而不是字符串
还可以用在服务器控件中
是换行。
见下面示例:
(说明:
需要先连接department库表。
depart_name,depart_num,picpath为字段名)
要准备几张照片)
1.添加模板
asp:
Repeaterid="
Repeater1"
runat="
server"
>
系部名称:
depart_name"
系部代码:
depart_num"
照片:
imgsrc='
picpath"
/>
<
TextBoxID="
TextBox1"
Text='
/asp:
TextBox>
br/>
/ItemTemplate>
HeaderTemplate>
h3>
模板页眉<
/h3>
/HeaderTemplate>
FooterTemplate>
模板页脚<
/FooterTemplate>
SeparatorTemplate>
hr>
/SeparatorTemplate>
Repeater>
2、数据库的连接利用类
使用连接类DB.CS来集中连接数据库,连接的代码方法如下:
returnnewSqlConnection("
DataBase=Northwind;
在每个页面中连接数据库时,加入下列代码即可。
SqlConnectionconn=DB.createCon();
3、设置代码
repeaterControl控件实现分页
privatevoidPage_Load(objectsender,System.EventArgse)
if(!
this.IsPostBack)
{
this.Label2.Text="
1"
this.data_Repeater();
//一般的,无分页的。
//this.databindToRepeater();
;
//带分页代码的
}
//在此处放置用户代码以初始化页面
定义方法(函数):
databindToRepeater()
privatevoiddata_Repeater()//一般的,无分页的。
SqlConnectioncon=DB.createCon();
SqlDataAdaptersda=newSqlDataAdapter("
select*fromDepartment"
con);
DataSetds=newDataSet();
sda.Fill(ds,"
emp"
this.Repeater1.DataSource=ds.Tables["
].DefaultView;
this.Repeater1.DataBind();
privatevoiddatabindToRepeater()//带分页代码的(可不讲)
//分页
intcurPage=Convert.ToInt32(this.Label2.Text);
//用此法类型转换
SqlConnectioncon=DB.createCon();
SqlDataAdaptersda=newSqlDataAdapter("
select*fromexam_database"
con);
//sda.SelectCommand=newSqlCommand("
select*fromEmployees"
DataSetds=newDataSet();
sda.Fill(ds,"
//分页
//System.Web.UI.WebControls.PagedDataSourceps=newPagedDataSource();
System.Web.UI.WebControls.PagedDataSourceps=newPagedDataSource();
ps.DataSource=ds.Tables["
ps.AllowPaging=true;
ps.PageSize=3;
ps.CurrentPageIndex=curPage-1;
this.Button1.Enabled=true;
this.Button2.Enabled=true;
if(curPage==1)
this.Button1.Enabled=false;
if(curPage==ps.PageCount)
this.Button2.Enabled=false;
//this.Repeater1.DataSource=ds.Tables["
this.Repeater1.DataSource=ps;
//改为ps,可分页的。
“上一页”命令按钮
privatevoidButton1_Click(objectsender,System.EventArgse)
this.Label2.Text=Convert.ToString(Convert.ToInt32(this.Label2.Text)-1);
this.databindToRepeater();
“下一页”命令按钮
privatevoidButton2_Click(objectsender,System.EventArgse)
this.Label2.Text=Convert.ToString(Convert.ToInt32(this.Label2.Text)+1);
this.databindToRepeater();
2、GridView控件
(1)使用传统的ADO.NET和GridView来做一些基本的操作。
实现分页、编辑、删除、排序、选中行、下拉列表框等基本操作。
//连接数据库使用上面讲过的类来连接
SqlConnectioncon=DB.createCon();
protectedvoidPage_Load(objectsender,EventArgse)
Page.IsPostBack)
//在此处放置用户代码以初始化页面
BindGrid();
//绑定子程序
protectedvoidBindGrid()
SqlDataAdapterdataAdapter2=newSqlDataAdapter("
select*fromcourse"
DataSetsetS=newDataSet();
dataAdapter2.Fill(setS,"
AAA"
this.GridView1.DataSource=setS.Tables["
this.GridView1.DataBind();
//数据绑定
//实现分页,注意事件的选用
protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse)
//分页
this.GridView1.PageIndex=e.NewPageIndex;
BindGrid();
//数据绑定
==================================================================
2012-11-14日,3,4节讲到此。
当时是把数据源控件对照着讲的,讲的网站是StorWeb。
实现删除仅开了个头。
//实现删除,
protectedvoidGridView1_RowDeleting(objectsender,GridViewDeleteEventArgse)
//删除,注意有一隐藏列:
ID
//stringkc_id=GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim();
//stringsqlstr="
deletefromcoursewhereCourse_id='
+kc_id+"
stringsqlstr="
+GridView1.DataKeys[e.RowIndex].Value.ToString()+"
SqlCommandcmd=newSqlCommand(sqlstr,myConn);
cmd.ExecuteNonQuery();
myConn.Close();
可为删除加提示:
确实要删除吗?
先要把此字段转换为TemplateField(模版列)然后编辑模版列,显示删除命令按钮的属性,在OnClientClick中写入:
)的提示。
这是再点击“删除”时,可出现提示。
//实现编辑和更新,可选择对应列编辑
protectedvoidGridView1_RowEditing(objectsender,GridViewEditEventArgse)
//编辑
this.GridView1.EditIndex=e.NewEditIndex;
protectedvoidGridView1_RowUpdating(objectsender,GridViewUpdateEventArgse)
//更新,注意到若在page_load事件中无:
if(!
Page.IsPostBack),则是不能更新的。
stringempID=this.GridView1.DataKeys[e.RowIndex].Value.ToString();
stringkc_name=((Text