| |

VerySource

 Forgot password?
 Register
Search
View: 804|Reply: 8

Old question, please pass by to see

[Copy link]

2

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-10 14:00:01
| Show all posts |Read mode
public void CreatTable ()
    {
        upfiles = new DataTable ("upfile");
        DataColumn [] keys = new DataColumn [1];
        keys [0] = upfiles.Columns.Add ("filename", typeof (string));
        keys [0] .AllowDBNull = false;
        upfiles.PrimaryKey = keys;
        upfiles.Columns.Add ("filestream", typeof (Stream));
        fname.Text = "";
    }
    /// <summary>
    /// add attachment to list
    /// </ summary>
    private void AddFile ()
    {
        if (upfiles == null)
        {
            CreatTable ();
        }
        if (myFile.PostedFile.FileName! = "")
        {
            string nam = myFile.PostedFile.FileName;
            int i = nam.LastIndexOf ("\\");
            string filename = nam.Substring (i + 1);
            if (upfiles.Rows.Find (filename) == null)
            {
                Stream fstream = myFile.PostedFile.InputStream;
                DataRow dr = upfiles.NewRow ();
                dr [0] = filename; // Get the file name
                upfiles.Rows.Add (dr); // increase
                ArrayList dc = new ArrayList ();
                for (int j = 0; j <upfiles.Rows.Count; j ++)
                {
                    DataRow df = upfiles.Rows [j];
                    dc.Add (df [0] .ToString ());
                }
                files.DataSource = dc;
                files.DataBind ();
            }
        }
    }
    /// <summary>
    /// remove attachment from list
    /// </ summary>
    private void RemoveFile ()
    {
        if (files.SelectedItem.Value! = null)
        {
            string fname = files.SelectedItem.Value;
            DataRow foundRow = upfiles.Rows.Find (fname);
            if (foundRow! = null)
                upfiles.Rows.Remove (foundRow);
            ArrayList dc = new ArrayList ();
            for (int j = 0; j <upfiles.Rows.Count; j ++)
            {
                DataRow df = upfiles.Rows [j];
                dc.Add (df [0] .ToString ());
            }
            files.DataSource = dc;
            files.DataBind ();
        }
    }
    /// <summary>
    /// send email
    /// </ summary>
    private void UploadFile ()
    {
        string userId = this.GetSessionState ("userId");
        if (upfiles.Rows.Count> 0)
        {
            fname.Text = "";
            for (int j = 0; j <upfiles.Rows.Count; j ++)
            {
                DataRow dr = upfiles.Rows [j];
                string topath = Server.MapPath ((this.GetSessionState ("userId") + "/") + dr [0]); // Set attachment path
                fname.Text + = topath.ToString () + ";";
                myFile.PostedFile.SaveAs (topath); // Save attachment

    }

When I insert "multi-attachment upload address" fname.Text into the database, the following happens:
For example: When I upload 3 files at a time, 3 rows of records are generated in the database:
A: / aaa;
A: / aaa; B: / bbb;
A: / aaa; B: / bbb; C: / ccc;
And what I want is to generate only one row of records in the database for each upload, such as:
A: / aaa; B: / bbb; C: / ccc;
Please help me.
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-6-2 10:00:02
| Show all posts
Can't see the statement you inserted into the record
Reply

Use magic Report

2

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-6-2 17:15:01
| Show all posts
Just a sentence of insert, I did not post it, multi-attachment upload address =fname.Text
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-6-2 21:45:03
| Show all posts
for (int j = 0; j <upfiles.Rows.Count; j++)
                {
                    DataRow df = upfiles.Rows[j];
                    dc.Add(df[0].ToString());
                }

Construct a string inside the loop, increase outside the loop
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-6-2 22:45:01
| Show all posts
Just a sentence of insert, I did not post it, multi-attachment upload address =fname.Text


Update outside the loop.

That is, all events are finished. Just update the database.
Reply

Use magic Report

0

Threads

22

Posts

18.00

Credits

Newbie

Rank: 1

Credits
18.00

 China

Post time: 2020-6-3 14:30:07
| Show all posts
For example: When I upload 3 files at a time, 3 rows of records are generated in the database:
A:/aaa;
A:/aaa;B:/bbb;
A:/aaa;B:/bbb;C:/ccc;
What I want is to generate only one row of records in the database for each upload, such as:
A:/aaa;B:/bbb;C:/ccc;
------------------------------------
When you traverse to the last attachment, the record inserted into the database is correct. Presumably you made the insertion inside the loop. In fact, you only need to construct the path string (A:/aaa; B:/bbb; C:/ccc; ) Insert just outside the loop, you can look at the code of your database operation
Reply

Use magic Report

0

Threads

21

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-6-3 18:00:01
| Show all posts
Use stringbuilder.appendtext to append filename every time.

dr[0] = filename, filename should be stringbuilder.tostring
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-3 19:00:01
| Show all posts
Yes, operate the database outside the loop, add a judgment in front, and add it outside the FOR
Reply

Use magic Report

2

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-6-7 00:00:01
| Show all posts
Thank you, brother, solved
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list