473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# and MSSQL (image type column)

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
3 4785
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: badz | last post by:
Hi frenz Lately I try to use MSSQL and PHP , the problem arise when PHP try to read MSSQL field with 'image' data type, header("Content-type: image/jpeg"); // act as a jpg file to browser I...
1
by: sumGirl | last post by:
Hi all. We have a mix of informix and mssql server and I want to know if something we do in informix has an analogous feature in MSSQL. We can define a "row type" in informix, like so: create...
14
by: Kukurydz | last post by:
I've got such problem: My database is stored on MSSQL Server. I have to write reports for it in MSAccess. I've got a problem with creating a query which will select records from MSSQL table with...
1
by: Dmitri Shvetsov | last post by:
Hi, What's the easiest method to do that? I need to extract an image from Image type column and then display it. Should I store the image as an image file on a disk and then assign the image URL...
5
by: moondaddy | last post by:
I'm caching a dataset in an asp.net session variable to hold a user's data. one data item I need to store is an image the user uploaded. My problem is that I don't know how to get the image into...
11
by: ralphie | last post by:
hi all since nearly 2 days i fight with mssql and utf-8 as i need to store and retrieve arabic characters. i tried the com approach ...
0
by: ruchika mahajan | last post by:
Hi all...MSSQL serevr 2000 Trace Event Column ObjectType gives value 17 for object table for events Object:Created and Object:deleted.Can anyone tell me all the possible values for MSSQL 2000 Trace...
3
by: Shawn Beasley | last post by:
Hi List, I am searching franticly for a solution (or the procedure) to setting the coding of a new DB to UTF-8. I can find no setting in the Server Manager, during creation of the DB, to...
0
by: chromis | last post by:
Hi, I've recently been porting a little php / mysql script over to asp / mssql and I've got issues with the timestamp data type. With mysql i could easily insert 01:01:01 into the timestamp...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.