Solved

bulk insert (image data type)

Posted on 2000-03-13
2
386 Views
Last Modified: 2008-03-04
Trying to figure out the best way to insert an image file into a table. Is this possible through the query analyzer with
bulk insert...
from c:\picture.bmp

or does this have to be done in a binary format...??
Thanks.
0
Comment
Question by:Your_Momma
2 Comments
 
LVL 8

Accepted Solution

by:
chigrik earned 100 total points
ID: 2613367
1. The following text shows, how you can insert the image from a file (you may use textcopy utility for this purpose).


CREATE TABLE dbo.tbImage (
        ID int IDENTITY (1, 1) NOT NULL ,
        fImage image NULL ,
        CONSTRAINT PK_Image PRIMARY KEY  CLUSTERED
        (
                ID
        )
)
GO

CREATE PROCEDURE sp_imageins(
  @srvname     varchar (30),
  @user_id     varchar (30),
  @password    varchar (30),
  @dbname      varchar (30),
  @tbname      varchar (30),
  @colname     varchar (30),
  @filename    varchar (30),
  @vhereclause varchar (40))
AS
DECLARE @execstring varchar (255)
insert into tbImage (fImage) values ('1')
select @execstring =
         'textcopy /S ' + @srvname +
         ' /U ' + @user_id +
         ' /P ' + @password +
         ' /D ' + @dbname +
         ' /T ' + @tbname +
         ' /C ' + @colname +
         ' /W "' + @vhereclause +
         '" /F ' + @filename +
         ' /I'
exec master..xp_cmdshell @execstring
GO


Parameters for example (from my computer):

sp_imageins 'ALEXANDER', 'sa', 'author', 'test', 'tbImage', 'fImage', 'c:\winnt\winnt.bmp', ' WHERE ID=1 '

2. Another way.
You can use AppendChunk() and GetChunk() method.
Location: SQL Server Books Online
Title:    Using Long Data Types

It's example in ADO (from SQL Server Books Online):

"
This example shows how to use ADO with SQLOLEDB to read and write image data. The critical routines are the while loops that copy the long data (image) to a variable and write the variable to a record in chunks (using the GetChunk and AppendChunk methods).


Before setting up the destination table in this example, make sure to run the sp_dboption stored procedure:

EXEC sp_dboption 'pubs', 'Select into/bulkcopy', 'True'

The destination table is a copy of the pub_info table in the pubs database. Create the table by running:

USE pubs

SELECT * INTO pub_info_x

   FROM pub_info

GO


The pub_info_x table is the destination table in which the long data will be inserted.

The ADO code is:

Public Sub AppendChunkX()

 

    Dim cn As ADODB.Connection

    Dim rstPubInfo As ADODB.Recordset

    Dim strCn As String

    Dim strPubID As String

    Dim strPRInfo As String

    Dim lngOffset As Long

    Dim lngLogoSize As Long

    Dim varLogo As Variant

    Dim varChunk As Variant

 

    Const conChunkSize = 100

 

    ' Open a connection.

    Set cn = New ADODB.Connection

    strCn = "Server=srv;Database=pubs;UID=sa;Pwd=;"

 

   cn.Provider = "sqloledb"

   cn.Open strCn

 

   'Open the pub_info_x table.

   Set rstPubInfo = New ADODB.Recordset

   rstPubInfo.CursorType = adOpenDynamic

   rstPubInfo.LockType = adLockOptimistic

   rstPubInfo.Open "pub_info_x", cn, , , adCmdTable

 

   'Prompt for a logo to copy.

   strMsg = "Available logos are : " & vbCr & vbCr

 

   Do While Not rstPubInfo.EOF

      strMsg = strMsg & rstPubInfo!pub_id & vbCr & _

        Left(rstPubInfo!pr_info,

         InStr(rstPubInfo!pr_info, ",") - 1) & vbCr & vbCr

      rstPubInfo.MoveNext

   Loop

 

   strMsg = strMsg & "Enter the ID of a logo to copy:"

   strPubID = InputBox(strMsg)

 

   ' Copy the logo to a variable in chunks.

   rstPubInfo.Filter = "pub_id = '" & strPubID & "'"

   lngLogoSize = rstPubInfo!logo.ActualSize

   Do While lngOffset < lngLogoSize

      varChunk = rstPubInfo!logo.GetChunk(conChunkSize)

      varLogo = varLogo & varChunk

      lngOffset = lngOffset + conChunkSize

   Loop

 

   ' Get data from the user.

   strPubID = Trim(InputBox("Enter a new pub ID:"))

   strPRInfo = Trim(InputBox("Enter descriptive text:"))

 

   ' Add a new record, copying the logo in chunks.

   rstPubInfo.AddNew

   rstPubInfo!pub_id = strPubID

   rstPubInfo!pr_info = strPRInfo

   lngOffset = 0   ' Reset offset.

 

   Do While lngOffset < lngLogoSize

      varChunk = LeftB(RightB(varLogo, lngLogoSize - _

        lngOffset),conChunkSize)

      rstPubInfo!logo.AppendChunk varChunk

      lngOffset = lngOffset + conChunkSize

   Loop

 

   rstPubInfo.Update

 

   ' Show the newly added data.

   MsgBox "New record: " & rstPubInfo!pub_id & vbCr & _

     "Description: " & rstPubInfo!pr_info & vbCr & _

     "Logo size: " & rstPubInfo!logo.ActualSize

 

   rstPubInfo.Close

   cn.Close

 

End Sub
"
0
 

Author Comment

by:Your_Momma
ID: 2613395
Not quite what I was looking for.
Using :
exec master..xp_cmdshell @execstring

is not really an option. But thanks anyway... :-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question