.net笔记(一)

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# 追加文件
StreamWriter sw = File.AppendText(Server.MapPath(".")+"
//myText.txt ");
sw.WriteLine("
追逐理想 ");
sw.WriteLine("kzlll");
sw.WriteLine(".NET
笔记
");
sw.Flush();
sw.Close();

C# 拷贝文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"
//myText.txt ";
NewFile = Server.MapPath(".")+"
//myTextCopy.txt ";
File.Copy(OrignFile,NewFile,true);

C# 删除文件
string delFile = Server.MapPath(".")+"
//myTextCopy.txt ";
File.Delete(delFile);

C# 移动文件
string OrignFile,NewFile;
OrignFile = Server.MapPath(".")+"
//myText.txt ";
NewFile = Server.MapPath(".")+"
//myTextCopy.txt ";
File.Move(OrignFile,NewFile);

C# 创建目录
//
创建目录
c:/sixAge
DirectoryInfo d=Directory.CreateDirectory("c://sixAge");
// d1
指向
c:/sixAge/sixAge1
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");
// d2
指向
c:/sixAge/sixAge1/sixAge1_1
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");
//
将当前目录设为
c:/sixAge
Directory.SetCurrentDirectory("c://sixAge");
//
创建目录
c:/sixAge/sixAge2
Directory.CreateDirectory("sixAge2");
//
创建目录
c:/sixAge/sixAge2/sixAge2_1
Directory.CreateDirectory("sixAge2//sixAge2_1");

递归删除文件夹及文件
<%@ Page Language=C#%>
<%@ Import namespace="System.IO"%>
<Script runat=server>
public void DeleteFolder(string dir)
{
    if (Directory.Exists(dir)) //
如果存在这个文件夹删除之

    {
        foreach(string d in Directory.GetFileSystemEntries(dir))
        {
            if(File.Exists(d))
                File.Delete(d); //
直接删除其中的文件

            else
                DeleteFolder(d); //
递归删除子文件夹

        }
        Directory.Delete(dir); //
删除已空文件夹

        Response.Write(dir+"
文件夹删除成功
");
    }
    else
        Response.Write(dir+"
该文件夹不存在 "); // 如果文件夹不存在则提示

}

protected void Page_Load (Object sender ,EventArgs e)
{
    string Dir="D://gbook//11";
    DeleteFolder(Dir); //
调用函数删除文件夹

}

 
// ======================================================
  //
实现一个静态方法将指定文件夹下面的所有内容 copy 到目标文件夹下面

  //
如果目标文件夹为只读属性就会报错。
  // April 18April2005 In STU
  // ======================================================
  public static void CopyDir(string srcPath,string aimPath)
  {
   try
   {
    //
检查目标目录是否以目录分割字符结束如果不是则添加之
    if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
     aimPath += Path.DirectorySeparatorChar;
    //
判断目标目录是否存在如果不存在则新建之
    if(!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
    //
得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
    //
如果你指向 copy 目标文件下面的文件而不包含目录请使用下面的方法
    // string[] fileList = Directory.GetFiles(srcPath);
    string[] fileList = Directory.GetFileSystemEntries(srcPath);
    //
遍历所有的文件和目录
    foreach(string file in fileList)
    {
     //
先当作目录处理如果存在这个目录就递归 Copy 该目录下面的文件
     if(Directory.Exists(file))
      CopyDir(file,aimPath+Path.GetFileName(file));
      //
否则直接 Copy 文件
     else
      File.Copy(file,aimPath+Path.GetFileName(file),true);
    }
   }
   catch (Exception e)
   {
    MessageBox.Show (e.ToString());
   }
  }

  // ======================================================
  //
实现一个静态方法将指定文件夹下面的所有内容
Detele
  //
测试的时候要小心操作,删除之后无法恢复。

  // April 18April2005 In STU
  // ======================================================
  public static void DeleteDir(string aimPath)
  {
   try
   {
    //
检查目标目录是否以目录分割字符结束如果不是则添加之
    if(aimPath[aimPath.Length-1] != Path.DirectorySeparatorChar)
     aimPath += Path.DirectorySeparatorChar;
    //
得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
    //
如果你指向 Delete 目标文件下面的文件而不包含目录请使用下面的方法
    // string[] fileList = Directory.GetFiles(aimPath);
    string[] fileList = Directory.GetFileSystemEntries(aimPath);
    //
遍历所有的文件和目录
    foreach(string file in fileList)
    {
     //
先当作目录处理如果存在这个目录就递归 Delete 该目录下面的文件
     if(Directory.Exists(file))
     {
      DeleteDir(aimPath+Path.GetFileName(file));
     }
      //
否则直接 Delete 文件
     else
     {
      File.Delete (aimPath+Path.GetFileName(file));
     }
    }
    //
删除文件夹
    System.IO .Directory .Delete (aimPath,true);
   }
   catch (Exception e)
   {
    MessageBox.Show (e.ToString());
   }
  }
 

 

需要引用命名空间:
using System.IO;

/** <summary>
  ///
拷贝文件夹 ( 包括子文件夹 ) 到指定文件夹下 , 源文件夹和目标文件夹均需绝对路径 . 格式 : CopyFolder( 源文件夹 , 目标文件夹
);
  /// </summary>
  /// <param name="strFromPath"></param>
  /// <param name="strToPath"></param>

  public static void CopyFolder(string strFromPath,string strToPath)
  {
   //
如果源文件夹不存在,则创建

   if (!Directory.Exists(strFromPath))
   {   
    Directory.CreateDirectory(strFromPath);
   }  

   // 取得要拷贝的文件夹名
   string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("//") + 1,strFromPath.Length - strFromPath.LastIndexOf("//") - 1);  

   // 如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹
   if (!Directory.Exists(strToPath + "//" + strFolderName))
   {   
    Directory.CreateDirectory(strToPath + "//" + strFolderName);
   }
   //
创建数组保存源文件夹下的文件名
   string[] strFiles = Directory.GetFiles(strFromPath);

   // 循环拷贝文件
   for(int i = 0;i < strFiles.Length;i++)
   {
    //
取得拷贝的文件名,只取文件名,地址截掉。
    string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("//") + 1,strFiles[i].Length - strFiles[i].LastIndexOf("//") - 1);
    //
开始拷贝文件 ,true 表示覆盖同名文件
    File.Copy(strFiles[i],strToPath + "//" + strFolderName + "//" + strFileName,true);
   }
 
   //
创建 DirectoryInfo 实例
   DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
   //
取得源文件夹下的所有子文件夹名称
   DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
   for (int j = 0;j < ZiPath.Length;j++)
   {
    //
获取所有子文件夹名
    string strZiPath = strFromPath + "//" + ZiPath[j].ToString();  
    //
把得到的子文件夹当成新的源文件夹,从头开始新一轮的拷贝
    CopyFolder(strZiPath,strToPath + "//" + strFolderName);
   }
  }

 

注意

1.        web.config 中的一个 / 相当于两个 //

ASP.NET笔记博客源码是一个用于创建和管理个人或团队博客的开源项目。它基于ASP.NET框架,使用C#作为主要编程语言。该项目的目的是为用户提供一个简单易用的平台,方便他们记录和分享个人或团队的经验、知识和想法。 在ASP.NET笔记博客源码中,主要包含以下几个核心模块: 1. 用户认证和授权:该模块负责用户的注册、登录和权限管理。用户可以通过创建自己的账户来使用博客系统,并根据不同的权限设置来管理自己的博客内容,如发布、编辑和删除博文等。 2. 博文管理:该模块用于管理博客的文章。用户可以使用富文本编辑器来撰写文章,并可以对文章进行分类、标签和搜索。此外,用户还可以管理自己的评论和回复。 3. 主题和样式:该模块允许用户自定义博客的主题和样式。用户可以选择不同的主题模板,如布局、颜色等,以满足自己的需求和喜好。 4. 评论和交互:用户可以对其他人的文章进行评论和回复,并可以与其他用户进行互动和讨论。这增加了博客的交互性和社交性。 此外,ASP.NET笔记博客源码还可以进行扩展和定制,以满足特定需求。用户可以添加其他功能模块,如图片库、标签管理和站点统计等,以实现更加丰富和个性化的博客。 总结来说,ASP.NET笔记博客源码是一个基于ASP.NET框架的开源项目,用于创建和管理个人或团队的博客。它提供了用户认证、博文管理、主题样式和评论交互等核心功能,同时支持扩展和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值