黑马程序员_学习日记53_625三层项目(FTP相关操作、递归TreeView、三层结构)

来源:互联网 发布:傲剑护身符升级数据 编辑:程序博客网 时间:2024/06/09 22:41

1、通过WebClient操作ftp

WebClient是对WebResponse和WebRequest的封装

//下载

WebClient wc = new WebClient();

//Credentials属性 获取或设置发送到主机并用于对请求进行身份验证的网络凭据

wc.Credentials = new NetworkCredential("yzk","1234");

wc.DownLoadFile("ftp://192.168.1.100/.gif",@"C:\a.gif");

cwl("ok");

//上传

WebClient wc = new WebClient();

wc.Credentials = new NetworkCredential("yzk","1234");

wc.UploadFile("ftp://192.168.1.100/a.mkv",@"C:\我相信.mkv");

cwl("ok");

crk();

2、FtpClient类的操作

//下载

FtpClient fc = new FtpClient("192.168.1.100","yzk","1234");

fc.Download("浅析MD5算法.doc",@"c:\x.doc");

//上传

FtpClient fc = new FtpClient("192.168.1.100","yzk","1234");

fc.MakeDirectory("xxx");//创建服务器目录

//"C:\1.mkv"

//Upload方法的第一个参数是FileInfo类的

FileInfo file = new FileInfo(@"C:\1.mkv");

fc.Upload(file,"xxx/test.mkv");

3、写Excel上传工具(WinForm)

3.1写配置文件

<configuration>

         <appSettings>

                   <add key="ip" value="192.168.1.100"/>

                   <add key="ip" value="192.168.1.100"/>

                   <add key="ip" value="192.168.1.100"/>

         </appSettings>

</configuration>

3.2

btnUpload_Click()

{

         string ip = ConfigurationManager.AppSettings["ip"];

         string uid = ConfigurationManager.AppSettings["uid"];

         string pwd = ConfigurationManager.AppSettings["pwd"];

        

         FtpClient fc = new FtpClient(ip,uid,pwd);

         string serverFileName = "01_"+DateTime.Now.ToString()...

         fc.Upload(new FileInfo(...))

}

4、定时自动下载

4.1配置文件

4.2

string ip = ConfigurationManager.AppSettings["ip"];

string uid = ConfigurationManager.AppSettings["uid"];

string pwd = ConfigurationManager.AppSettings["pwd"];

 

FtpClient fc = new FtpClient(ip,uid,pwd);

string serverFileName = "01_"+DateTime.Now.ToString("yyyyMMdd")...

//判断文件是否存在

if(fc.CheckFileExist())

{

         bool b = fc.Download(serverFileName,...)

}

5、拼音检索(面试侃点)

//遍历用户输入的每个char

StringBuilder sbPinyin = new StringBuilder();

foreach (char chin txtChn.Text)

{

         ChineseChar chnChar = new ChineseChar(ch);

         //foreach (string py in chnChar.Pinyins)

         //{

         //    sbPinyin.Append(py + "\r\n");

         //}

         //去掉空行

         //for (int i = 0; i < chnChar.PinyinCount; i++)

         //{

         //    string pinyin = chnChar.Pinyins[i].Substring(0, chnChar.Pinyins[i].Length - 1);

         //    sbPinyin.Append(pinyin + "\r\n");

         //}

         //只显示每组拼音的第一个

         string pinyin = chnChar.Pinyins[0].Substring(0, chnChar.Pinyins[0].Length - 1);               

         sbPinyin.Append(pinyin + "\r\n");

}

txtPinyin.Text = sbPinyin.ToString();

6、为T_Customers表更新拼音字段

6.1配置文件(constr)

6.2从数据库查数据,更新加入拼音

string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

string sql = "select CC_AutoId,CC_CustomerName from T_Customers";

string sql2 = "update T_Customers set CC_NamePinyin=@NamePinyin,CC_NamePyShort=@NamePyShort";

SqlParameter[] pams = new SqlParameter[2];

using (SqlDataReader reader = SqlHelper.ExecuteReader(constr, System.Data.CommandType.Text, sql))

{

         if (reader.HasRows)

         {

                   while (reader.Read())

                   {

                            int autoId = reader.GetInt32(0);

                            string name = reader.IsDBNull(1) ?null : reader.GetString(1);

                            string pinyin = CommonHelper.GetPinyinByString(name);

                            string pyShort = CommonHelper.GetShortPinyinByString(name);

 

                            pams[0] = new SqlParameter("@NamePinyin", pinyin);

                            pams[1] = new SqlParameter("@NamePyShort", pyShort);

                            SqlHelper.ExecuteNonQuery(constr, System.Data.CommandType.Text, sql2, pams);

                   }

         }

}

Console.WriteLine("插入拼音成功!");

Console.ReadKey();

6.3在CommonHelper中增加根据字符串获得拼音的功能

//根据姓名获得拼音

public staticstring GetPinyinByString(string str)

{

         StringBuilder sbPinyin = new StringBuilder();

         foreach (char chin str)

         {

                   ChineseChar chnChar = new ChineseChar(ch);

                   string chPinyin = chnChar.Pinyins[0].Substring(0,chnChar.Pinyins[0].Length-1);

                   sbPinyin.Append(chPinyin);

         }

         return sbPinyin.ToString();

}

//根据姓名获得拼音缩写

public staticstring GetShortPinyinByString(string str)

{

         StringBuilder sbPinyin = new StringBuilder();

         foreach (char chin str)

         {

                   ChineseChar chnChar = new ChineseChar(ch);

                   string chPinyin = chnChar.Pinyins[0].Substring(0,1);

                   sbPinyin.Append(chPinyin);

         }

         return sbPinyin.ToString();

}

 

7、递归

7.1递归一定要有条件,否则死递归。

7.2尾递归计算fibonacci数列

static void Main(string[] args)

{

         Fibonacci(50);

}

 

static int Fibonacci(int i)

{

         if (i == 1)

                   return 1;

         if (i == 2)

                   return 1;

         else

                   return Fibonacci(i - 1) + Fibonacci(i - 2);

}

7.3递归加载TreeView

//获取父Id0的省份与直辖市

List<Area> list = GetDataByParentId(0);

//List中的数据绑定到TreeView

LoadDataToTree(treeView1.Nodes,list);

 

private void LoadDataToTree(TreeNodeCollection treeNodeCollection,List<Area> list)

{

         foreach(Area item in list)

         {

                   TreeNode tnode = treeNodeCollection.Add(item.AreaName);

                   //记录当前区域的Id

                  tnode.Tag = item.AreaId;

                   //获取当前Id对应的所有子城市

                   List<Area> listSub = GetDataByParentId(item.AreaId);

                   LoadDataToTree(tnode.Nodes,listSub);

         }

}

 

//根据父Id获取该区域下的所有子区域

private List<Area> GetDataByParentId(int pid)

{

         List<Area> list = new List<Area>();

         string sql = "select * form TblArea where AreaPId=@pid";

}

7.4递归删除TreeView选中节点及子节点

//首先获取用户选择节点的Id

if(treeView1.SelectedNode != null)

{

         int areaId = (int)treeView1.SelectedNode.Tag;

         //根据areaId删除当前元素

         DeleteByAreaId(areaId);

         //TODO:更新界面

}

else

{

        

}

 

private void DeleteByAreaId(int areaId)

{

         //查询areaId的子元素

         List<Area> list = GetDataByParentId(areaId);

         foreach(Area item in list)

         {

                   DeleteByAreaId(item.AreaId);

         }

         //TODO:通过Sql语句删除

}

 

8、三层架构

表现层UI:数据采集、把数据库返回的数据显示出来(UI层调用BLL层)

业务逻辑层BLL:做一些校验、判断、根据不同数据返回不同结果、判断用户是否锁定

数据访问层DAL:拼接sql语句,出现各种数据访问的类,比如Connection、Command、

 

案例:通过三层实现年龄自动增长

三个项目分别实现三层

8.1将窗体建在表现层,配置文件在表现层

 

8.2SqlHelper在数据访问层

在数据访问层建MyStudentDal.cs类,对MyStudent表执行增删改查

//数据访问层的方法注意:

//1sql语句、访问数据库的类,只能出现在这一层

//2、数据访问层职责要单一,不要做过多的操作。

public int IncAgeById(int fid)

{

         string sql = "update MyStudent set fage=fage+1 where fid=@id";

         SqlParameter p1 = new SqlParameter("@id",fid);

         return SqlHelper.ExecuteNonQuery(sql,p1);

}

8.3业务逻辑层

建MyStudentBll.cs类

//业务逻辑层的方法返回什么值,取决于表现层要什么结果

public bool IncAgeById(int fid)

{

         MyStudentDal dal = new MyStudentDal();

         int r=dal.IncAgeById(fid);

         if(r>0)

         {

                   return true;

         }

         else

         {

                   return false;

         }

}

 

作业:

将Excel从服务器直接传到数据库(网络流转内存流)

尾递归和循环分别输出斐波那契数列