用image类型方法:
1、建立过程
CREATE PROCEDURE sp_textcopy ( 
  @srvname    varchar (30), 
  @login      varchar (30), 
  @password    varchar (30), 
  @dbname      varchar (30), 
  @tbname      varchar (30), 
  @colname    varchar (30), 
  @filename    varchar (30), 
  @whereclause varchar (40), 
  @direction  char(1)) 
AS 
DECLARE @exec_str varchar (255) 
SELECT @exec_str = 
        'textcopy /S ' + @srvname + 
        ' /U ' + @login + 
        ' /P ' + @password + 
        ' /D ' + @dbname + 
        ' /T ' + @tbname + 
        ' /C ' + @colname + 
        ' /W "' + @whereclause + 
        '" /F ' + @filename + 
        ' /' + @direction 
EXEC master..xp_cmdshell @exec_str  2、建表和初始化数据
create table 表名 (编号 int,image列名 image)
go
insert 表名 values(1,0x)
insert 表名 values(2,0x)
go3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2go4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
go如果报textcopy不是可执行文件的话,你就到
C:\Program Files\Microsoft SQL Server\MSSQL\Binn
目录下拷备 textcopy.exe到:
C:\Program Files\Microsoft SQL Server\80\Tools\Binn

解决方案 »

  1.   

    用image类型方法:
    1、建立过程
    CREATE PROCEDURE sp_textcopy ( 
      @srvname    varchar (30), 
      @login      varchar (30), 
      @password    varchar (30), 
      @dbname      varchar (30), 
      @tbname      varchar (30), 
      @colname    varchar (30), 
      @filename    varchar (30), 
      @whereclause varchar (40), 
      @direction  char(1)) 
    AS 
    DECLARE @exec_str varchar (255) 
    SELECT @exec_str = 
            'textcopy /S ' + @srvname + 
            ' /U ' + @login + 
            ' /P ' + @password + 
            ' /D ' + @dbname + 
            ' /T ' + @tbname + 
            ' /C ' + @colname + 
            ' /W "' + @whereclause + 
            '" /F ' + @filename + 
            ' /' + @direction 
    EXEC master..xp_cmdshell @exec_str  2、建表和初始化数据
    create table 表名 (编号 int,image列名 image)
    go
    insert 表名 values(1,0x)
    insert 表名 values(2,0x)
    go3、读入
    sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2go4、读出成文件
    sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
    go
      

  2.   

    /*--bcp-二进制文件的导入导出 支持image,text,ntext字段的导入/导出
    image适合于二进制文件;text,ntext适合于文本数据文件 注意:导入时,将覆盖满足条件的所有行
    导出时,将把所有满足条件的行也出到指定文件中 此存储过程仅用bcp实现
    邹建 2003.08-----------------*//*--调用示例
    --数据导出
    exec p_binaryIO 'zj','','','acc_演示数据..tb','img','c:\zj1.dat'--数据导出
    exec p_binaryIO 'zj','','','acc_演示数据..tb','img','c:\zj1.dat','',0
    --*/
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_binaryIO]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[p_binaryIO]
    GOCreate proc p_binaryIO
    @servename varchar (30),--服务器名称
    @username varchar (30), --用户名
    @password varchar (30), --密码
    @tbname varchar (500),  --数据库..表名
    @fdname varchar (30),  --字段名
    @fname varchar (1000), --目录+文件名,处理过程中要使用/覆盖:@filename+.bak
    @tj varchar (1000)='',  --处理条件.对于数据导入,如果条件中包含@fdname,请指定表名前缀
    @isout bit=1 --1导出((默认),0导入
    AS 
    declare @fname_in varchar(1000) --bcp处理应答文件名
    ,@fsize varchar(20) --要处理的文件的大小
    ,@m_tbname varchar(50) --临时表名
    ,@sql varchar(8000)--则取得导入文件的大小
    if @isout=1 
    set @fsize='0'
    else
    begin
    create table #tb(可选名 varchar(20),大小 int
    ,创建日期 varchar(10),创建时间 varchar(20)
    ,上次写操作日期 varchar(10),上次写操作时间 varchar(20)
    ,上次访问日期 varchar(10),上次访问时间 varchar(20),特性 int)
    insert into #tb
    exec master..xp_getfiledetails @fname
    select @fsize=大小 from #tb
    drop table #tb
    if @fsize is null
    begin
    print '文件未找到'
    return
    endend--生成数据处理应答文件
    set @m_tbname='[##temp'+cast(newid() as varchar(40))+']'
    set @sql='select * into '+@m_tbname+' from(
    select null as 类型
    union all select 0 as 前缀
    union all select '+@fsize+' as 长度
    union all select null as 结束
    union all select null as 格式
    ) a'
    exec(@sql)
    select @fname_in=@fname+'_temp'
    ,@sql='bcp "'+@m_tbname+'" out "'+@fname_in
    +'" /S"'+@servename
    +case when isnull(@username,'')='' then '' 
    else '" /U"'+@username end
    +'" /P"'+isnull(@password,'')+'" /c'
    exec master..xp_cmdshell @sql
    --删除临时表
    set @sql='drop table '+@m_tbname
    exec(@sql)if @isout=1
    begin
    set @sql='bcp "select top 1 '+@fdname+' from ' 
    +@tbname+case isnull(@tj,'') when '' then ''
    else ' where '+@tj end
    +'" queryout "'+@fname
    +'" /S"'+@servename
    +case when isnull(@username,'')='' then '' 
    else '" /U"'+@username end
    +'" /P"'+isnull(@password,'')
    +'" /i"'+@fname_in+'"'
    exec master..xp_cmdshell @sql
    end
    else
    begin
    --为数据导入准备临时表
    set @sql='select top 0 '+@fdname+' into '
    +@m_tbname+' from ' +@tbname
    exec(@sql) --将数据导入到临时表
    set @sql='bcp "'+@m_tbname+'" in "'+@fname
    +'" /S"'+@servename
    +case when isnull(@username,'')='' then '' 
    else '" /U"'+@username end
    +'" /P"'+isnull(@password,'')
    +'" /i"'+@fname_in+'"'
    exec master..xp_cmdshell @sql

    --将数据导入到正式表中
    set @sql='update '+@tbname
    +' set '+@fdname+'=b.'+@fdname
    +' from '+@tbname+' a,'
    +@m_tbname+' b'
    +case isnull(@tj,'') when '' then ''
    else ' where '+@tj end
    exec(@sql) --删除数据处理临时表
    set @sql='drop table '+@m_tbname
    end--删除数据处理应答文件
    set @sql='del '+@fname_in
    exec master..xp_cmdshell @sqlgo
      

  3.   

    我想是用adodb.stream的read方法获得二进制数据,然后直接用insert sql 语句效率好些。以上方法试用过,感觉处理起来很慢。
      

  4.   

    http://www.csdn.net/Develop/Read_Article.asp?Id=17699
      

  5.   

    下面的代码实现向SQL Server数据库添加图片和文字的功能。首先,在SQL查询分析器中执行下面的SQL语句,以创建表和存储过程。CREATE TABLE Photos (
    [name] varchar(50),
    [photo] image NULL
    )
    GOCREATE PROCEDURE sp_InsertPhoto
    @name AS VARCHAR(50),
    @image AS IMAGE
    AS
    INSERT INTO Photos ([name],  [photo])
    VALUES (@name, @image)
    GO
    下面就是完整的代码,拷贝即可运行:Imports System.IO
    Public Class Form1
      Inherits System.Windows.Forms.Form
      Dim cn As SqlClient.SqlConnection#Region " Windows Form Designer generated code "  Public Sub New()
        MyBase.New()    'This call is required by the Windows Form Designer.
        InitializeComponent()    'Add any initialization after the InitializeComponent() call  End Sub  'Form overrides dispose to clean up the component list.
      Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
          If Not (components Is Nothing) Then
            components.Dispose()
          End If
        End If
        MyBase.Dispose(disposing)
      End Sub  'Required by the Windows Form Designer
      Private components As System.ComponentModel.IContainer  'NOTE: The following procedure is required by the Windows Form Designer
      'It can be modified using the Windows Form Designer.
      'Do not modify it using the code editor.
      Friend WithEvents Label1 As System.Windows.Forms.Label
      Friend WithEvents Label2 As System.Windows.Forms.Label
      Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
      Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
      Friend WithEvents Button1 As System.Windows.Forms.Button
      Friend WithEvents Button2 As System.Windows.Forms.Button
      Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
      Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
      Friend WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
      Friend WithEvents Button3 As System.Windows.Forms.Button
      Friend WithEvents Button4 As System.Windows.Forms.Button
      Friend WithEvents Button5 As System.Windows.Forms.Button
      <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
        Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.Button4 = New System.Windows.Forms.Button()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(15, 19)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 24)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "姓名:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(11, 64)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(80, 20)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "图片"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(75, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(173, 20)
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = ""
        '
        'PictureBox1
        '
        Me.PictureBox1.Location = New System.Drawing.Point(68, 48)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(376, 222)
        Me.PictureBox1.TabIndex = 3
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(278, 13)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 26)
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "浏览图片…"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(57, 277)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(100, 32)
        Me.Button2.TabIndex = 5
        Me.Button2.Text = "添加到数据库"
        '
        'SqlConnection1
        '
        Me.SqlConnection1.ConnectionString = "Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;"
        '
        'SqlCommand1
        '
        Me.SqlCommand1.CommandText = "dbo.[sp_InsertPhoto]"
        Me.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure
        Me.SqlCommand1.Connection = Me.SqlConnection1
        Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", _
            System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, _
            False, CType(10, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing))
        Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@name", _
            System.Data.SqlDbType.VarChar, 50))
        Me.SqlCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@image", _
            System.Data.SqlDbType.VarBinary, 2147483647))
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(265, 277)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(79, 32)
        Me.Button3.TabIndex = 6
        Me.Button3.Text = "显示记录"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(362, 277)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(72, 32)
        Me.Button4.TabIndex = 7
        Me.Button4.Text = "退出"
        '
        'Button5
      

  6.   

    '
        Me.Button5.Location = New System.Drawing.Point(167, 277)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(85, 32)
        Me.Button5.TabIndex = 8
        Me.Button5.Text = "删除该记录"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(491, 324)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1, _
            Me.Button5, Me.Button4, Me.Button3, Me.Button2, Me.Button1, Me.TextBox1, Me.Label1, Me.Label2})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)  End Sub#End Region
      'Browse Button
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button1.Click
        'Display Picture File
        OpenFileDialog1.InitialDirectory = "d:\pic"
        OpenFileDialog1.DefaultExt = "gif"
        OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"
        OpenFileDialog1.ShowDialog()
        PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
      End Sub  'Add Button
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button2.Click
        ' To Insert Image
        Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
        Dim s As String = TextBox1.Text
        Dim mbr As BinaryReader = New BinaryReader(st)
        Dim buffer(st.Length) As Byte
        mbr.Read(buffer, 0, CInt(st.Length))
        st.Close()
        InsertImage(buffer, s)
      End Sub  'Function For Inserting in the Procdeure in the Database
      Public Function InsertImage(ByRef buffer, ByVal str)
        cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)
        cn.Open()
        Dim cmd As New SqlClient.SqlCommand("sp_InsertPhoto", cn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text
        cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer
        cmd.ExecuteNonQuery()
        MsgBox("Image inserted")
        cn.Close()
      End Function  'Function to Display Image
      Private Sub ShowImage(ByVal s As String)
        cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)
        cn.Open()
        Dim str As String = "SELECT photo FROM Photos WHERE name='" & s & "'"
        Dim cmd As New SqlClient.SqlCommand(str, cn)
        TextBox1.Text = s
        Dim b() As Byte
        b = cmd.ExecuteScalar()
        If (b.Length > 0) Then
          Dim stream As New MemoryStream(b, True)
          stream.Write(b, 0, b.Length)
          DrawToScale(New Bitmap(stream))
          stream.Close()
        End If
        cn.Close()
      End Sub  'Function to Create Instance For the Image From the Buffer
      Private Sub DrawToScale(ByVal bmp As Image)
        PictureBox1.Image = New Bitmap(bmp)
      End Sub  '显示处理
      Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button3.Click
        Dim i As String = InputBox("请输入名字:")
        ShowImage(i)
      End Sub  '退出处理
      Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button4.Click
        Me.Dispose()
      End Sub  '删除处理
      Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles Button5.Click
        cn = New SqlClient.SqlConnection(SqlConnection1.ConnectionString)
        cn.Open()
        Dim s As String = InputBox("请输入要删除的名字:")
        Dim cmd As New SqlClient.SqlCommand("delete from photos where name='" & s & "'", cn)
        cmd.ExecuteNonQuery()
        MsgBox("Image deleted")
        cn.Close()
      End Sub
    End Class