需要循环遍历图片 [英] need to loop over the pictures

查看:58
本文介绍了需要循环遍历图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,用户可以附加图片,并且所有内容都保存在数据库中,下面的代码运行正常,但我需要让用户有可用性添加多个图片相同的窗口,当他附上照片时,用labellink.text = openfileDialog.Filename创建标签链接;



这里是代码



I'm trying to make a Program which user Could Attach Pictures,And All Is Saved In Database Under One Id ,The Below Code Is Running Correctly But I Need To Let The User Have The Availability To add more than one pic in the same window , and when he attach a photo a label link is created with labellink.text = openfileDialog.Filename;

here is the code

private void add_photos_Click(object sender, EventArgs e)
        {   
            DBClass db = DBClass.GetInstance(); //this is a class which contains all commands of database and the connection

            add_new_content aa = (add_new_content)this.Owner;
            string id = @"select cont_id from dbo.contents where cont_name='" + aa.txt_name.Text + "'";
            object obj = db.ExecuteScalre(id,CommandType.Text);


            string str = @"INSERT INTO [Picture_upload].[dbo].[Pictures]
           ([cont_id]
           ,[path])
            VALUES
           (" + obj + ",'" +
              openFileDialog1.FileName + "')";
            

            db.ExecuteNonQuery(str, CommandType.Text); // i want to loop it

            linkLabel1.Text = openFileDialog1.FileName;  //i want to loop this too
            txt_link.Text = " ";



     }

推荐答案

看看你对你的问题的评论,它看起来你想要每次按下按钮将文件添加到数据库中 - 在这种情况下你根本不想要一个循环:你每次按下按钮都会得到一个Click事件,所以每次他都做一个按下它。



然后,唯一的复杂因素是:

1)你需要在点击处理程序中激活你的OpenFileDialog以获得一个新的file:

Looking at your comments on your question, it looks like you want each time he presses the button to add a file to the db - in which case you don't want a loop at all: you will get a Click event each time he presses the button, so do one each time he presses it.

Then, the only complications are:
1) You need to activate your OpenFileDialog in the click handler to get a new file:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
   {
   DBClass db = DBClass.GetInstance();
   ...
   }

虽然我可能会将OpenFileDialog实例创建为事件处理程序的一部分,而不是在我的表单中嵌入一个。

2)您需要在每次单击时添加一个新的LinkLabel - 如果您只是回收现有的LinkLabel,那么您只能显示最后一个文件链接。但是,你需要弄清楚你要把它们放在哪里 - 我不能为你做到 - 但一般代码是:

Though I would probably create the OpenFileDialog instance as part of the event handler, rather than embedding one in my form.
2) You need to look at adding a new LinkLabel each time he clicks - if you just recycle the existing one then you can only show the last file link. However, you need to work out where you are going to put them - I can't do that for you - but the general code is:

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
    {
    LinkLabel ll = new LinkLabel();
    ll.Text = ofd.FileName;
    ll.Location = new Point(100, 100);
    Controls.Add(ll);
    }

您需要将位置设置为您的位置(每次都不应该相同...),您可能还需要连接一些事件。 />
3)您还应该查看数据库代码:不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

You will need to set the location to your point (which shouldn't be the same each time...) and you probably need to hook up some events as well.
3) You should look at your database code as well: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


这篇关于需要循环遍历图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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