如何将文本文件数据从数据库检索到文本框?

编程入门 行业动态 更新时间:2024-10-28 11:29:54
本文介绍了如何将文本文件数据从数据库检索到文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

实际上我将一个文本文件上传到数据库,现在我需要将该数据放入文本框(多行) 我为文件上传编写的代码 =======================================

Actually i uploaded a text file to database and now i need to get that data into a textbox(multiline) code that i have written for file upload ========================================

protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile == null || string.IsNullOrEmpty(FileUpload1.PostedFile.FileName) || FileUpload1.PostedFile.InputStream == null) { Label1.Text = "<br />Error - unable to upload file. Please try again.<br />"; } else { string SQL = "INSERT INTO FileUpLoad values(@FileName, @DateTime, @MIME, @BinaryData)"; SqlCommand cmd = new SqlCommand(SQL, con); cmd.Parameters.AddWithValue("@FileName", TextBox2.Text.Trim()); cmd.Parameters.AddWithValue("@DateTime", DateTime.Now); cmd.Parameters.AddWithValue("@MIME", FileUpload1.PostedFile.ContentType); byte[] imageBytes = new byte[FileUpload1.PostedFile.InputStream.Length + 1]; FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length); cmd.Parameters.AddWithValue("@BinaryData", imageBytes); con.Open(); cmd.ExecuteNonQuery(); Label1.Text = "<br />File successfully uploaded - thank you.<br />"; con.Close(); } con.Close(); }

推荐答案

using(SqlCommand cmd = new SqlCommand("SELECT BinaryData FROM FileUpload", connection)) { byte[] file = (byte)cmd.ExexcuteScalar(); MemoryStream ms = new MemoryStream(file, true); ms.Write(file, 0, file.Length); using(StreamReader reader = new StreamReader(ms)) { textbox.Text = reader.ReadToEnd(); } }

更多推荐

如何将文本文件数据从数据库检索到文本框?

本文发布于:2023-07-09 00:17:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1082111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   文本文件   文本框   数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!