locked
how to upload a file in sqlserver 2000 databse RRS feed

  • Question

  • User1513216887 posted

    Hi,

     

    how to upload a file in sqlserver 2000 databse using file uploadcontrol in asp.net.

     advance thanks.

    Thanks

    anitha

    Friday, July 4, 2008 6:08 AM

Answers

  • User1735976268 posted

    The FileUpload control has a property called FileBytes. This will give you the byte[] for the image which can be stored in an SQL varbinary(MAX) field.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2008 9:34 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2008 9:41 AM
  • User-1734134863 posted

    Hi,

    Please try to run the following code  which I have written based on my understandings of your problem.

    .cs file

          protected void Button4_Click(object sender, EventArgs e)
            {
                string picType = this.FileUpload1.PostedFile.ContentType;
                if (picType != "")
                {
                    string picName = this.FileUpload1.FileName;
                    SqlConnection conn = new SqlConnection("server=.;database=Test;uid=sa;pwd=issac");
                    conn.Open();
                    SqlCommand comm = new SqlCommand("insert into test(image) values(@image)", conn);
                    comm.Parameters.Add("@image", SqlDbType.Image);      
                    comm.Parameters[0].Value = this.FileUpload1.FileBytes;
                    comm.ExecuteNonQuery();
                    conn.Close();
            
                }
                else
                {
    
                }
    
            }

     Hope this helps.

    Sincerely,
    Zong-Qing Li
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 10:33 PM

All replies

  • User1735976268 posted

    The FileUpload control has a property called FileBytes. This will give you the byte[] for the image which can be stored in an SQL varbinary(MAX) field.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2008 9:34 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 4, 2008 9:41 AM
  • User-1734134863 posted

    Hi,

    Please try to run the following code  which I have written based on my understandings of your problem.

    .cs file

          protected void Button4_Click(object sender, EventArgs e)
            {
                string picType = this.FileUpload1.PostedFile.ContentType;
                if (picType != "")
                {
                    string picName = this.FileUpload1.FileName;
                    SqlConnection conn = new SqlConnection("server=.;database=Test;uid=sa;pwd=issac");
                    conn.Open();
                    SqlCommand comm = new SqlCommand("insert into test(image) values(@image)", conn);
                    comm.Parameters.Add("@image", SqlDbType.Image);      
                    comm.Parameters[0].Value = this.FileUpload1.FileBytes;
                    comm.ExecuteNonQuery();
                    conn.Close();
            
                }
                else
                {
    
                }
    
            }

     Hope this helps.

    Sincerely,
    Zong-Qing Li
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 7, 2008 10:33 PM