C# and MSSQL (image type column)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to load an rtf file to my sql database (into image type column). now I
am using external tool textcopy.exe (launched within my application)
Is it possible (how to do that) to write my own application that will load a
file into image type column in sql db?
 
Chris said:
I need to load an rtf file to my sql database (into image type
column). now I am using external tool textcopy.exe (launched within
my application)
Is it possible (how to do that) to write my own application that will
load a file into image type column in sql db?

Of course! Something along these lines (untested code - may not even
compile):

using System;
using System.Data;
using System.Data.SqlClient;
using System.IO

class Loader
{
public static void Main(string[] args)
{
byte[] image = null;
using (FileStream fs = new FileStream(
args[0],
FileMode.Open,
FileAccess.Read,
FileShare.Read
))
{
image = new byte[fs.Length];
fs.Read(image,0,image.Length);
}
}

using (SqlConnection con = new SqlConnection("my connection string"))
{
con.Open();

SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "insert into MyTable (MyImageColumn) values
(@Image)";
cmd.Paramters.Add("@Image",SqlDbType.Image).Value = image;
cmd.ExecuteNonQuery();
}
}

Look up the various classes used (FileStream, SqlConnection, SqlCommand) in
MSDN for more info.

-cd
 
Chris said:
I need to load an rtf file to my sql database (into image type column). now I
am using external tool textcopy.exe (launched within my application)
Is it possible (how to do that) to write my own application that will load a
file into image type column in sql db?
If you construct the sql statement to do the insert using a
parameterized query then you can add the parameter which should be a
byte array read from the file.

JB
 
Back
Top