如何从SQL Server中选择数据并将其保存到计算机上的某个位置 [英] How to select data from SQL Server and save it to somewhere on my computer
问题描述
我已经从数据库中选择了二进制数据,然后我展示了一个SaveFileDialog来将二进制数据保存在我的计算机上。我编写了以下代码来做到这一点,但是当我打开保存的文件时,其中没有任何数据。它只是对txt文件正常工作。我明白问题在于以下几行:
I've selected binary data by its name from database then I showed a SaveFileDialog to save that binary data somewhere on my computer. I wrote the following code to do that but when I opened saved file, there is not any data in it.It just work correctly for txt file. I understood that the problem is in following line:
byte[] content = sdr["Content"] as byte[];
在任意大小的文件的下一行它只能读取200byte。例如我上传了文件367byte,它正确上传,但是当我下载它时,在下面的行中,内容是200byte。
IN following line for any size of file it just can read 200byte of it .for example i uploaded the file with 367byte, It uploaded correctly but when i downloaded it, in follwing line, content is 200byte.
private void btnDownload_Click(object sender, EventArgs e)
{
try
{
using (SqlCommand cmd = new SqlCommand("SelectFile", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
Conn.Open();
cmd.Parameters.Add("@FileName", SqlDbType.VarChar).Value = "1.txt";
string Ext = Path.GetExtension("1.txt");
SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (sdr.HasRows)
{
sdr.Read();
byte[] content = sdr["Content"] as byte[] ;
if (fdDownload.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(fdDownload.FileName + Ext , FileMode.CreateNew, FileAccess.Write);
fs.Write(content, 0, content.Length);
fs.Flush();
fs.Close();
}
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File Downloaded Successfully";
Conn.Close();
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "File not Downloaded Successfully";
Conn.Close();
}
}
}
catch (SqlException sqlEx)
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File not Downloaded Successfully";
}
}
推荐答案
你好朋友,
请访问以下文章,因为这可能对您的要求有所帮助:
在ASP.NET中使用SQL Server上载和下载文件 [ ^ ]
- DD
Hello friend,
please visit the following article as this may be helpful to your requirement:
Upload and Download Files with SQL Server in ASP.NET[^]
- DD
这篇关于如何从SQL Server中选择数据并将其保存到计算机上的某个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!