Home All Groups Group Topic Archive Search About

Upload/Download to/from a SQL db.

Author
26 Aug 2005 7:20 PM
JDArsenault
I have a requirement to be able to upload a file (let's say xls spreadsheet)
to a table, and then have someone else be able to download that same file,
modify it and re-upload it again. This is common place for SharePoint using
SQL, but my requirement doen't involve SharePoint. Any ideas, examples or
help from any of y'all?

Author
26 Aug 2005 8:08 PM
ML
Are you planning on designing a Content Management System? Or just need to
store binary files in SQL?

Please, elaborate.


ML
Author
26 Aug 2005 8:55 PM
JDArsenault
I'm trying to avoid anymore overhead (Designing a cm system). I'm looking to
store binary files if I can get away with it & still download the file to
modify it.

Thanks

Show quote
"ML" wrote:

> Are you planning on designing a Content Management System? Or just need to
> store binary files in SQL?
>
> Please, elaborate.
>
>
> ML
Author
26 Aug 2005 9:34 PM
ML
You basically need a table to store the files - in SQL 2000 using the image
data type and in SQL 2005 using the new varbinary(max) data type.

Full-text indexing supports XLS files if you need to implement full-text
search.

If you post more info regarding your requirements, we can provide more help.


ML
Author
26 Aug 2005 8:34 PM
David Browne
"JDArsenault" <JDArsena***@discussions.microsoft.com> wrote in message
news:98D4FCBE-3374-428D-84DE-39D6F63109B6@microsoft.com...
>I have a requirement to be able to upload a file (let's say xls
>spreadsheet)
> to a table, and then have someone else be able to download that same file,
> modify it and re-upload it again. This is common place for SharePoint
> using
> SQL, but my requirement doen't involve SharePoint. Any ideas, examples or
> help from any of y'all?

My idea is to use SharePoint.

But if you really can't, here's a sample .NET console application to upload
files into SQL Server:


using System;
using System.IO;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

namespace SqlFileUpload
{
  class Program
  {
    const string sqlSetup = @"
DROP TABLE MyImages
CREATE TABLE MyImages
(
  Name varchar(512) not null primary key,
  Description varchar(2000) null,
  Data image
)
";
    const string sqUploadFile = @"
INSERT INTO MyImages (Name,Description,Data)
VALUES (@Name, @Description, @Data)
";

    static void Main(string[] args)
    {
      Trace.Listeners.Add(new TextWriterTraceListener(System.Console.Out));

      try
      {
        UploadFolder(@"c:\download");
        Trace.WriteLine("Complete");
      }
      catch (Exception ex)
      {
        Trace.WriteLine(ex);
      }
    }
    static void UploadFolder(string Folder)
    {
      using (SqlConnection conn = new SqlConnection("Integrated
Security=SSPI;database=test1;server=(local)"))
      using (SqlCommand cmdInsert = new SqlCommand(sqUploadFile,conn))
      {
        conn.Open();
        try
        {
          new SqlCommand(sqlSetup, conn).ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
          Trace.WriteLine(ex.Message);
        }
        SqlParameter pName =
cmdInsert.Parameters.Add("@Name",SqlDbType.VarChar,512);
        SqlParameter pDescription =
cmdInsert.Parameters.Add("@Description",SqlDbType.VarChar,2000);
        SqlParameter pData =
cmdInsert.Parameters.Add("@Data",SqlDbType.Image);

        DirectoryInfo di = new DirectoryInfo(Folder);
        foreach (FileInfo f in di.GetFiles())
        {
          using (FileStream fs = f.OpenRead())
          {
            byte[] contents = new byte[fs.Length];
            fs.Read(contents,0,contents.Length);
            pName.Value = f.Name;
            pDescription.Value = f.FullName;
            pData.Value = contents;
            cmdInsert.ExecuteNonQuery();
            Trace.WriteLine(String.Format("Uploaded {0}, {1}kb", f.Name,
contents.Length/1024));
          }
        }
      }
    }
  }
}


David
Author
26 Aug 2005 8:58 PM
JDArsenault
Thanks...I'll give it a try.

Show quote
"David Browne" wrote:

>
> "JDArsenault" <JDArsena***@discussions.microsoft.com> wrote in message
> news:98D4FCBE-3374-428D-84DE-39D6F63109B6@microsoft.com...
> >I have a requirement to be able to upload a file (let's say xls
> >spreadsheet)
> > to a table, and then have someone else be able to download that same file,
> > modify it and re-upload it again. This is common place for SharePoint
> > using
> > SQL, but my requirement doen't involve SharePoint. Any ideas, examples or
> > help from any of y'all?
>
> My idea is to use SharePoint.
>
> But if you really can't, here's a sample .NET console application to upload
> files into SQL Server:
>
>
> using System;
> using System.IO;
> using System.Text;
> using System.Data.SqlClient;
> using System.Data;
> using System.Diagnostics;
>
> namespace SqlFileUpload
> {
>   class Program
>   {
>     const string sqlSetup = @"
> DROP TABLE MyImages
> CREATE TABLE MyImages
> (
>   Name varchar(512) not null primary key,
>   Description varchar(2000) null,
>   Data image
> )
> ";
>     const string sqUploadFile = @"
> INSERT INTO MyImages (Name,Description,Data)
> VALUES (@Name, @Description, @Data)
> ";
>
>     static void Main(string[] args)
>     {
>       Trace.Listeners.Add(new TextWriterTraceListener(System.Console.Out));
>
>       try
>       {
>         UploadFolder(@"c:\download");
>         Trace.WriteLine("Complete");
>       }
>       catch (Exception ex)
>       {
>         Trace.WriteLine(ex);
>       }
>     }
>     static void UploadFolder(string Folder)
>     {
>       using (SqlConnection conn = new SqlConnection("Integrated
> Security=SSPI;database=test1;server=(local)"))
>       using (SqlCommand cmdInsert = new SqlCommand(sqUploadFile,conn))
>       {
>         conn.Open();
>         try
>         {
>           new SqlCommand(sqlSetup, conn).ExecuteNonQuery();
>         }
>         catch (SqlException ex)
>         {
>           Trace.WriteLine(ex.Message);
>         }
>         SqlParameter pName =
> cmdInsert.Parameters.Add("@Name",SqlDbType.VarChar,512);
>         SqlParameter pDescription =
> cmdInsert.Parameters.Add("@Description",SqlDbType.VarChar,2000);
>         SqlParameter pData =
> cmdInsert.Parameters.Add("@Data",SqlDbType.Image);
>
>         DirectoryInfo di = new DirectoryInfo(Folder);
>         foreach (FileInfo f in di.GetFiles())
>         {
>           using (FileStream fs = f.OpenRead())
>           {
>             byte[] contents = new byte[fs.Length];
>             fs.Read(contents,0,contents.Length);
>             pName.Value = f.Name;
>             pDescription.Value = f.FullName;
>             pData.Value = contents;
>             cmdInsert.ExecuteNonQuery();
>             Trace.WriteLine(String.Format("Uploaded {0}, {1}kb", f.Name,
> contents.Length/1024));
>           }
>         }
>       }
>     }
>   }
> }
>
>
> David
>
>
>

AddThis Social Bookmark Button