By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

C# and MSSQL (image type column)

P: n/a
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?
Aug 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chris wrote:
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

Aug 15 '06 #2

P: n/a
A Google search will get you many examples.. Here is one:
http://sqljunkies.com/Article/4D0E6E...2D55C96E8.scuk

"Chris" <Ch***@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.com...
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?

Aug 15 '06 #3

P: n/a
Chris wrote:
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
Aug 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.