Home All Groups Group Topic Archive Search About

Storing files in SQL Server

Author
1 Sep 2005 5:14 PM
Dave
I need to store large xml files in my database.  I was going to use the text
datatype. Can someone point to some good how-to's on creating the SP as well
as the ADO.NET code? 

From what I read, if you use text, you need to do an INSERT first to create
the record then use the WRITETEXT function on that row's text column?  Thanks

Author
1 Sep 2005 5:25 PM
Aaron Bertrand [SQL Server MVP]
> From what I read, if you use text, you need to do an INSERT first to
> create
> the record then use the WRITETEXT function on that row's text column?

No, that is not true.

CREATE TABLE #foo(id INT IDENTITY(1,1), bar TEXT)
INSERT #foo(bar) SELECT 'here is text'
DROP TABLE #foo

There are articles you can easly find on Google for how to handle read/write
of files to SQL Server .  Have you researched the pros and cons?  Do you
understand all of the factors that go along with the choice of storing a
file in the database?  I only ask because most people who weigh both sides
opt for storing the file in the file system and the data about the file in
the database.

A
Author
1 Sep 2005 5:28 PM
David Browne
"Dave" <D***@discussions.microsoft.com> wrote in message
news:179E771D-FC44-4584-95D1-FFC66643CFED@microsoft.com...
>I need to store large xml files in my database.  I was going to use the
>text
> datatype. Can someone point to some good how-to's on creating the SP as
> well
> as the ADO.NET code?
>
> From what I read, if you use text, you need to do an INSERT first to
> create
> the record then use the WRITETEXT function on that row's text column?

AAK no.  That's only if you are doing it in manipulating Text values in
TSQL.

Here's some sample .NET code.  It uses Image not Text, but the idea is the
same.  Just change the type and bind a String, not a byte[].

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));
          }
        }
      }
    }
  }
}

AddThis Social Bookmark Button