1.ASP.NET 页面的基本对象
Application 、 Session 、 Response 、 Page 、
Request : Request.Form[ 数据名称 ] (取得 Post ) ;Request.QueryString[ 数据源名称 ]( 取得 Get) ;
Server :
MapPath()// 得到文件和图片的服务器路径
HtmlEncode()// 将字符串转化为 HTML 格式
URLEncode()// 将字符串转化为 URL
Cookie :
A.aspx:
HttpCookie myCookie = new HttpCookie ("userName" );
myCookie.Value = "dypzy" ;
Response.Cookies.Add(myCookie);
B:aspx:
Response.Write(Request.Cookies["userName" ].Value.ToString());
2. 与数据库交互
using System.Data.SqlClient;// 引入包
SqlConnection conn = new SqlConnection ();
conn.ConnectionString = "server=B3C34B6CCA724CA//SQLSERVER2000;database=db_xnhrm;uid=sa;pwd=sa" ; //SqlConnection conn = new SqlConnection("Server=.;uid=sa;pwd=sa;Database=db_xrhrm"); conn.Open();
//sql 语句 string sql = string .Format("insert into tb_amdin(name,pwd,quantity) values('{0}','{1}',{2})" ,this .name.Text.ToString(),this .pwd.Text.ToString(),this .quantity.Text.ToString());
Response.Write(sql); SqlCommand sqlcmd = new SqlCommand (sql,conn); sqlcmd.ExecuteNonQuery();
conn.Close(); Response.Write(conn.State.ToString()); |
2.1 返回单个值:
Int x = (int)sqlcmd.ExecuteScalar();
2.2 返回结果集:
SqlDataReader dr = sqlcmd.ExecuteReader();
while (dr.Read()) {
Response.Write(dr["name" ].ToString());
}
2.3 使用DataSet :
SqlDataAdapter sda = new SqlDataAdapter ("select * from tb_amdin" , conn); DataSet ds = new DataSet (); sda.Fill(ds, "tb_admin" ); GridView1.DataSource = ds.Tables["tb_admin" ]; GridView1.DataBind(); |
2.4 事务处理
SqlCommand sqlCmd = new SqlCommand(); SqlTransaction tx = conn..BeginTransaction(); sqlCmd.Connection = conn; sqlCmd.Transaction = tx; try { ….. tx.Commit(); } catch { tx.Rollback(); } finally {} |
3. 分层编程
1. 添加 bean 类 DB using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; // 导入包 using System.Data.SqlClient;
/// <summary> /// DB 的摘要说明 /// </summary> public class DB { private SqlConnection conn; private SqlDataAdapter sda; private DataSet ds; private SqlCommand sqlCmd;
public DB() { // // TODO: 在此处添加构造函数逻辑 // }
public DataSet getDs() { conn = new SqlConnection (); sda = new SqlDataAdapter (); ds = new DataSet (); conn.ConnectionString = "server=B3C34B6CCA724CA//SQLSERVER2000;database=db_xnhrm;uid=sa;pwd=sa" ; conn.Open(); sda.SelectCommand = new SqlCommand ("select * from tb_amdin" , conn); sda.Fill(ds, "admin" ); return ds; conn.Close(); }
public void update(int id,string name,string pwd,string quantity) { conn = new SqlConnection (); conn.ConnectionString = "server=B3C34B6CCA724CA//SQLSERVER2000;database=db_xnhrm;uid=sa;pwd=sa" ; conn.Open(); string sql = string .Format("update tb_amdin set name='{0}',pwd ='{1}',quantity ={2} where id={3}" ,name,pwd,quantity,id); SqlCommand sqlCmd = new SqlCommand (sql, conn); sqlCmd.ExecuteNonQuery(); } }
2. 在 web 层运用 ObjectDataSource 控件和 Bean 关联,最后在把 ObjectDataSource 和 gridview 结合 |
4.web 层数据显示控件
1.GridView 2.DetailsView :显示具体的信息 3.DataList 4.FormView |
5. 文件处理
C# 追加文件 C# 拷贝文件 C# 删除文件 C# 移动文件 C# 创建目录 递归删除文件夹及文件 protected void Page_Load (Object sender ,EventArgs e) // ======================================================
需要引用命名空间: /** <summary> public static void CopyFolder(string strFromPath,string strToPath) // 取得要拷贝的文件夹名 // 如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹 // 循环拷贝文件
|
注意
1. web.config 中的一个 / 相当于两个 //