日期:2014-05-18  浏览次数:20551 次

SQL的问题:怎样在SQL2000中实现文本读取,图片传入数据库表
怎样在SQL2000中实现文本读取,并 插入数据库中存储;怎样把图片传入数据库表中存储,再读取。 我要代码,菜鸟啊,没有办法。不够加分啊。

------解决方案--------------------
VB code
'在MSSQL中存儲文本,圖片一般放在前台,以二進制方式存儲,讀取的時候在轉換回來
Private Sub cmdSave_Click()
  If Trim(txtFile) = "" Then MsgBox "請選擇上傳文件!", vbExclamation, "系統提示!": Exit Sub
  On Error GoTo err_file
  Dim Jsr As String
  For i = 0 To Listfilename.ListCount - 1
      If Listfilename.Selected(i) = True Then
         Jsr = Jsr & Listfilename.List(i) & ";"
      End If
  Next i
  If Trim(Jsr) = "" Then MsgBox "接收人不能為空!", vbExclamation, "系統提示!": Exit Sub
  Jsr = Left(Jsr, Len(Jsr) - 1)
  Dim cn As ADODB.Connection
  Dim rs As ADODB.Recordset
  Dim mst As ADODB.Stream
  Me.MousePointer = 11
  Set cn = New ADODB.Connection
  cn.ConnectionString = ConnectString
  cn.Open
  Set rs = New ADODB.Recordset
  rs.Open "select * from tb_file", cn, adOpenStatic, adLockOptimistic
  Set mst = New ADODB.Stream
  mst.Type = adTypeBinary
  mst.Open
  mst.LoadFromFile txtFile.Text
  rs.AddNew
  rs.Fields(1).Value = Trim(txtSubject.Text)    '主題
  rs.Fields(2).Value = Trim(txtfiletext.Text)   '描述
  rs.Fields(3).Value = Trim(txtPerson.Text)     '發送人
  rs.Fields(4).Value = Trim(Jsr)                '接收人
  rs.Fields(5).Value = Trim(Format(dtpfsrq.Value, "yyyy-mm-dd"))
  rs.Fields(6).Value = mst.Read
  rs.Fields(7).Value = FileName
  rs.Update
  rs.Close
  cn.Close
  Me.MousePointer = 0
  MsgBox "文件發送成功!", vbInformation, "系統提示!"
err_file:
  If Err.Number <> 0 Then MsgBox Err.Description
End Sub

------解决方案--------------------
SQL code
USE Northwind
GO
CREATE TABLE TextParts (ColA INT PRIMARY KEY, ColB TEXT)
GO
INSERT INTO TextParts
   VALUES( 1,
           'Sample string START TAG Text to go END TAG Trailing text.')
GO
DECLARE @PtrVar BINARY(16)
DECLARE @InsertPos INT
DECLARE @DeleteLen INT

SELECT @PtrVar = TEXTPTR(ColB),
       @InsertPos = (PATINDEX('%START TAG%', ColB) + 9),
       @DeleteLen = (
                      PATINDEX('%END TAG%', ColB) -
                      ( PATINDEX('%START TAG%', ColB) + 9
                              + 2 /* allow for blanks */ )
                    )
FROM TextParts
WHERE ColA = 1

UPDATETEXT TextParts.ColB
           @PtrVar
           @InsertPos
           @DeleteLen
           WITH LOG
           'The new text'
GO

SELECT * FROM TextParts
GO

/*
ColA        ColB
----------- ------------------------
1           Sample string START TAG The new text END TAG Trailing text.

*/

------解决方案--------------------
SQL code

--文本读取
create table tb(col varchar(8000))

bulk insert tb
from 'c:\abcd.txt'

select * from tb

drop table tb

------解决方案--------------------
[code=C#][/code]
int FileLen = this.PhotoFul.PostedFile.ContentLength; //获取上传文件的大小
Byte[] FileDate = new Byte[FileLen];
HttpPostedFile hpf = PhotoFul.PostedFile; //创建访问客户端上传文件的对象
Stream str = hpf.InputStream; //创建数据流对象
str.Read(FileDate, 0, FileLen); /*将图片数据放到FileData数组中,其中0代表数组指针的起始位置,
FileLen表示要读取流的长度(指针的结束位置)*/
student.PhotoUrl = FileDate;

图片上传
------解决方案--------------------
C# code

int FileLen = this.PhotoFul.PostedFile.ContentLength;    //获取上传文件的大小
Byte[] FileDate = new Byte[FileLen];
HttpPostedFile hpf = PhotoFul.PostedFile;  //创建访问客户端上传文件的对象
Stream str = hpf.InputStream;  //创建数据流对象
str.Read(FileDate, 0, FileLen);  /*将图片数据放到FileData数组中,其中0代表数组指针的起始位置,
                                                     FileLen表示要读取流的长度(指针的结束位置)*/
student.PhotoUrl = FileDate;