任何人请逐行解释此代码 [英] Any one please explain this code line by line

查看:62
本文介绍了任何人请逐行解释此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图多次将图像文件上传到MySql,但是我没有正确完成.所以最后我从CP获得了工作代码.它可以运行,但是我想逐步了解该过程.任何人都可以逐行解释以下代码吗?

I''m trying to upload an image file into MySql many times, but I did''t get it right. So finally I got working code from CP. It runs, but I want to understand the process step by step. Can anyone please explain the following code line by line?

    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.Files.Count != 0)
        {
            HttpPostedFile httpFile = Request.Files[0];
            string extension = this.GetFileExtension(httpFile.ContentType);
            if(extension == null )
            {
                Response.Write("Mime type not Supported");
                return;
            }
            BufferedStream bf = new BufferedStream(httpFile.InputStream);
            byte[] buffer = new byte[bf.Length];
            bf.Read(buffer,0,buffer.Length);
            DataAccess data = new DataAccess();
            data.addImage(buffer,extension);
            Response.Write("Image Added!");
        }
    }

    private string GetFileExtension(string p)
    {
        return p;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    }
    
    public class DataAccess
    {
        MySqlConnection con = new MySqlConnection("user id=root; password=admin; database=qms; server=localhost");
        public string addImage(byte[] buffer, string extension)
        {
            string strSql = "SELECT * FROM image";
            DataSet ds = new DataSet("Image");
            MySqlDataAdapter tempAP = new MySqlDataAdapter(strSql, this.con);
            MySqlCommandBuilder objCommand = new MySqlCommandBuilder(tempAP);
            tempAP.Fill(ds, "Table");

            try
            {
                this.con.Open();
                DataRow objNewRow = ds.Tables["Table"].NewRow();
                objNewRow["Extension"] = extension;
                objNewRow["Data"] = buffer;
                ds.Tables["Table"].Rows.Add(objNewRow);
                // trying to update the table to add the image
                tempAP.Update(ds, "Table");
            }
            catch (Exception e) { return e.Message; }
            finally { this.con.Close(); }
            return null;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    }

}



感谢您的帮助!



Thanks for your help!

推荐答案

如果您无法分析未编写的现有代码,那么您如何期望开发出类似于技能"的东西?关于成为一名程序员?
If you can''t analyze existing code that you didn''t write, how do you ever expect to develop anything resembling "skill" with regards to being a programmer?


逐行?您知道需要多少工作吗?

这意味着每行代码至少要写一个句子,而其中一些行将需要一段或一段左右的时间来完整地解释它们.

尝试详细描述如何制作一杯茶,当您完成时,它会变冷(可能发霉)!

请尝试并解释您的问题所在,我们可能会为您提供帮助.

是的,我的问题是y,我们将dataaccess和if(Request.Files.Count!= 0)这行请解释...".

Line by Line? Do you have any idea how much work that involves?

It means writing a minimum of one sentence per line of code, and some of those lines will require a paragraph or so to explain them fully.

Try describing how to make a cup of tea in that much detail, and it will have gone cold (and probably mouldy) by the time you are finished!

Try and explain what your problem areas are instead, and we may be able to help.

"yes my ques is y we put dataaccess and if(Request.Files.Count != 0) this line plz explain...."

if(Request.Files.Count != 0)


如果您不了解某些内容,并且其中包含."字符,则将其分解并使用google查看MSDN:
Request是一个对象,它检索客户端浏览器传递给服务器的值: MSDN [ ^ ]
Count是所有Collections的标准属性,它告诉您其中包含多少个项目.

所以Request.Files.Count告诉您上传了多少文件.

如果该数字不为零,则立即在下面执行代码块.

(您现在看到了,解释一个完整的程序需要多少工作,即使是那么小的程序?:)


If you don''t understand something and it has ''.'' characters in it, then break it down and look at MSDN with google:
Request is an object which retrieves the values that the client browser passed to the server: MSDN[^]
So Request.Files is an HttpRequest (which is what Request is an instance of) property : MSDN[^]
And Count is a standard property of all Collections, which tells you how many items it contains.

So Request.Files.Count tells you how many files were uploaded.

If this number is not zero, then execute the code block immediately below.

(Do you see now, how much work is involved in explaining an entire program, even one that small? :)


这篇关于任何人请逐行解释此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆