VB.NET code
SQL COde
- Dim con As New SqlConnection _
- ("Server=YourServer;uid=<username>;pwd=<strong 解password>;database=northwind")
- Dim da As New SqlDataAdapter _
- ("Select * From MyImages", con)
- Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
- Dim ds As New DataSet()
- da.MissingSchemaAction = MissingSchemaAction.AddWithKey
- Dim fs As New FileStream _
- ("C:/winnt/Gone Fishing.BMP", FileMode.OpenOrCreate, _
- FileAccess.Read)
- Dim MyData(fs.Length) As Byte
- fs.Read(MyData, 0, fs.Length)
- fs.Close()
- con.Open()
- da.Fill(ds, "MyImages")
- Dim myRow As DataRow
- myRow = ds.Tables("MyImages").NewRow()
- myRow("Description") = "This would be description text"
- myRow("imgField") = MyData
- ds.Tables("MyImages").Rows.Add(myRow)
- da.Update(ds, "MyImages")
- fs = Nothing
- MyCB = Nothing
- ds = Nothing
- da = Nothing
- con.Close()
- con = Nothing
- MsgBox ("Image saved to database")
- Double-click Button2, and then add the following code to the Button2_Click event handler:
- Note You must change uid <username> and pwd =<strong password> to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database.
- Dim con As New SqlConnection _
- ("Server=YourServer;uid=<username>;pwd=<strong password>;database=northwind")
- Dim da As New SqlDataAdapter _
- ("Select * From MyImages", con)
- Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
- Dim ds As New DataSet()
- con.Open()
- da.Fill(ds, "MyImages")
- Dim myRow As DataRow
- myRow = ds.Tables("MyImages").Rows(0)
- Dim MyData() As Byte
- MyData = myRow("imgField")
- Dim K As Long
- K = UBound(MyData)
- Dim fs As New FileStream _
- ("C:/winnt/Gone Fishing2.BMP", FileMode.OpenOrCreate, _
- FileAccess.Write)
- fs.Write(MyData, 0, K)
- fs.Close()
- fs = Nothing
- MyCB = Nothing
- ds = Nothing
- da = Nothing
- con.Close()
- con = Nothing
- MsgBox ("Image retrieved")
- 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) -- 必须的,且不是null
- insert 表名 values(2,0x) -- 必须的,且不是null
- go
- 3、读入
- sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:/图片.bmp','where 编号=1','I' --注意条件是 编号=1
- sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:/bb.doc','where 编号=2','I' --注意条件是 编号=2
- go
- 4、读出成文件
- sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:/图片.bmp','where 编号=1','O' --注意条件是 编号=1
- sp_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