web.config and split 用法

一:通常我们把数据库的连接语句放在web.config中,因为这样便于修改,如果让在类库中,这样生成了dll就不方便修改了。
比如在web.config中增加数据库连接语句:

<connectionStrings>

<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;User ID=sa;Password=sa;Initial Catalog=votetest;"/>

</connectionStrings>

在类先要导入 using System.Configuration;(一般情况下增加类的时候自动导入)

 

然后ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString,ok了
ConfigurationManager是表示web.config类对象 然后引用ConnectionStrings。。。

 

二:split用法:split表示分离字符串;

如:string str="hello| world"
string strSplite=str.split('|')
则 strSplit[0]="hello";

strSoplit[1]="world"

下面以一个在线编辑的图片上传利用web.config修改图片的上传的文件夹以及大小
先看web。config:

<appSettings>

    <add key="Insert_picture_editor_parameters" value="upload|204800" />

  </appSettings>
然后看:c#源文件:

void upfile()

    {

        #region

        Random myrd = new Random();

        string path, filename, upfilepath; //上传文件路径,上传文件名称

        string[] temps =ConfigurationSettings.AppSettings["Insert_picture_editor_parameters"].ToString().Split('|');


        path = Server.MapPath("~/"+temps[0]+"/");

        filename = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + myrd.Next(1000).ToString(); //上传文件名称


        //取得文件的扩展名

        string fileExtension = System.IO.Path.GetExtension(File1.PostedFile.FileName).ToLower();

        path = path + filename + fileExtension;

        if (fileExtension != ".jpg" && fileExtension != ".gif" && fileExtension != ".jpeg" && fileExtension != ".png")

        {

            string js = @"<Script language='JavaScript'>alert('错误!!文件类型必须为jpg或者gif或者png!');</Script>";

            HttpContext.Current.Response.Write(js);

        }

        else if (File1.PostedFile.ContentLength > int.Parse(temps[1]))

        {

            string js = @"<Script language='JavaScript'>alert('错误!!文件大小不能超过" + temps[1] + "字节!');</Script>";(temps[1]就是204800)

            HttpContext.Current.Response.Write(js);

        }

        else

        {

            upfilepath = temps[0] + "/" + filename + fileExtension;//前面出掉了 "/" +  (temp[0]就是split分离的第一个字符串也就是upload)

            File1.PostedFile.SaveAs(path);


            string js = @"<script language=javascript>";

            js += " window.returnValue =  '<img src= \"" + upfilepath + "\"/>';";

            js += "window.close();";

            js += "</script>";

            Response.Write(js);

        }

 

 





转载于:https://www.cnblogs.com/zxlin25/archive/2009/03/10/1407744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值