SQL的问题:怎样在SQL2000中实现文本读取,图片传入数据库表
怎样在SQL2000中实现文本读取,并 插入数据库中存储;怎样把图片传入数据库表中存储,再读取。 我要代码,菜鸟啊,没有办法。不够加分啊。
[解决办法]
'在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.DescriptionEnd Sub
[解决办法]
USE NorthwindGOCREATE TABLE TextParts (ColA INT PRIMARY KEY, ColB TEXT)GOINSERT INTO TextParts VALUES( 1, 'Sample string START TAG Text to go END TAG Trailing text.')GODECLARE @PtrVar BINARY(16)DECLARE @InsertPos INTDECLARE @DeleteLen INTSELECT @PtrVar = TEXTPTR(ColB), @InsertPos = (PATINDEX('%START TAG%', ColB) + 9), @DeleteLen = ( PATINDEX('%END TAG%', ColB) - ( PATINDEX('%START TAG%', ColB) + 9 + 2 /* allow for blanks */ ) )FROM TextPartsWHERE ColA = 1UPDATETEXT TextParts.ColB @PtrVar @InsertPos @DeleteLen WITH LOG 'The new text'GOSELECT * FROM TextPartsGO/*ColA ColB----------- ------------------------1 Sample string START TAG The new text END TAG Trailing text.*/
[解决办法]
--文本读取create table tb(col varchar(8000))bulk insert tbfrom 'c:\abcd.txt'select * from tbdrop 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;
图片上传
[解决办法]
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;
[解决办法]
跟前台程序相关性大些。
找一种语言读数据到stream流里写到image或binary列就可以了。
[解决办法]
create table #(t varchar(2000))goinsert into #(t) exec master..xp_cmdshell 'dir c:\*.*'select * from # drop table #
[解决办法]
--文本读取
create table tb(col varchar(8000))
bulk insert tb
from 'c:\abcd.txt'
select * from tb
drop table tb