请问如何将文件转成流存入数据库?

weilt 2004-07-26 08:22:30
开发工具,VS2003,数据据,ORACLE 8.1.7.
谢谢
...全文
579 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weilt 2004-07-27
  • 打赏
  • 举报
回复
如果是WORD文件呢?读出的时候怎 么做呢?
andrawsky 2004-07-27
  • 打赏
  • 举报
回复
Stream fileDataStream = MyFile.PostedFile.InputStream;

//得到文件大小
int fileLength = MyFile.PostedFile.ContentLength;

//创建数组
byte[] fileData = new byte[fileLength];

//把文件流填充到数组
fileDataStream.Read(fileData,0,fileLength);

//得到文件名字
string fileTitle = MyFileName.Value;

//得到文件类型
string fileType = MyFile.PostedFile.ContentType;

//构建数据库连接,SQL语句,创建参数
SqlConnection connection = new SqlConnection("Server=127.0.0.1;uid=sa;pwd=gotomis;Database=shop");
SqlCommand command = new SqlCommand ("INSERT INTO TestFiles (MyFileName,MyFile,FileType)" +
"VALUES (@MyFileName,@MyFile,@FileType)", connection);

SqlParameter paramTitle = new SqlParameter ("@MyFileName", SqlDbType.VarChar,35);
paramTitle.Value = fileTitle;
command.Parameters.Add(paramTitle);

SqlParameter paramData = new SqlParameter ("@MyFile", SqlDbType.Image);
paramData.Value = fileData;
command.Parameters.Add(paramData);

SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,25);
paramType.Value = fileType;
command.Parameters.Add(paramType);

//打开连接,执行查询
connection.Open();
command.ExecuteNonQuery();
connection.Close();

Message.Text="你的文件已经成功上载";
MyFileName.Value = "";
SeeSunSet 2004-07-27
  • 打赏
  • 举报
回复
Dim cnn As New OracleConnection(G_strCnn)
Dim com As New OracleCommand
If strParent = Nothing Or strParent = "" Then
com.CommandText = "Select WJ,WJMC FROM documents WHERE wjlx='" & strchapter & "' And JH='" & strjh & "' And zy='" & strtype & "'"
Else
com.CommandText = "Select WJ,WJMC FROM documents WHERE wjlx='" & strchapter & "' And JH='" & strjh & "' And zy='" & strtype & "' And parentnodeid='" & strParent & "'"
End If
com.Connection = cnn
Dim dr As OracleDataReader
Dim bFile() As Byte
Try
cnn.Open()
dr = com.ExecuteReader
If dr.Read Then
ReDim bFile(dr.GetOracleLob(0).Length)
strDiskFileName = Server.MapPath(".") & "\FileCache\" & dr.Item(1)
Me.txtFileName.Text = strDiskFileName
strHttpFileNameAs = "./FileCache/" & dr.Item(1)
dr.GetOracleLob(0).Read(bFile, 0, dr.GetOracleLob(0).Length)
Dim oFile As New System.IO.FileStream(strDiskFileName, IO.FileMode.Create)
oFile.Write(bFile, 0, bFile.Length)
oFile.Flush()
oFile.Close()
Else
lblDisplayReport.Text = "请添加相关的报告!"
End If

Catch ex As Exception
Response.Write(ex.ToString)
Finally
dr.Close()
dr.Dispose()
com.Dispose()
cnn.Close()
cnn.Dispose()
End Try
strFilePath = "files"
Dim strScript As String
strScript = "<script language='javascript'>document.frames(""iframereport"").location.href=""" & strHttpFileNameAs & """;</script>"
lvzm 2004-07-26
  • 打赏
  • 举报
回复
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
string HD_Type=this.hdlb.SelectedValue.Trim();
try
{
if (FileLength == 0)
{ //文件长度为零时
Message.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray,0,FileLength);
//建立SQL Server链接

SqlConnection Con = new SqlConnection();
Con = a.myConnection();
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription,ImageContent, ImageSize) VALUES (@Image, @ContentType, @ImageDescription,@ImageContent, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = HD_Type; //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = txtDescription.Text;
CmdObj.Parameters.Add("@ImageContent", SqlDbType.NVarChar,3000).Value = txtContent.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
Message.Text = "<p><b>OK!你已经成功上传你的图片</b>";//提示上传成功
hdlb.SelectedItem.Selected=false;
txtDescription.Text="";

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧