如何从SQL Server中选择数据并将其保存到我的计算机

本文关键字:保存 计算机 我的 数据 SQL Server 选择 | 更新日期: 2023-09-27 18:08:49

我根据数据库的名称选择了二进制数据,然后我显示了一个SaveFileDialog以将该二进制数据保存在我的计算机上。我写了下面的代码来做到这一点,但是当我打开保存的文件时,里面没有任何数据。它只是工作正确的txt文件。我该如何解决这个问题?

 private void btnUpload_Click(object sender, EventArgs e)
    {
        fdTransfer.ShowDialog();
        string filePath = fdTransfer.FileName;
        string fileName=Path.GetFileName(filePath);
        string Ext=Path.GetExtension(fileName);
        string contentType = string.Empty;
        switch (Ext)
        {
            case ".doc":
                contentType = "application/vnd.ms-word";
                break;
            case ".docx":
                contentType = "application/vnd.ms-word";
                break;
            case ".txt":
                contentType = "application/vnd.ms-txt";
                break;
            case ".xls":
                contentType = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                contentType = "application/vnd.ms-excel";
                break;
            case ".jpg":
                contentType = "image/jpg";
                break;
            case ".png":
                contentType = "image/png";
                break;
            case ".gif":
                contentType = "image/gif";
                break;
            case ".pdf":
                contentType = "application/pdf";
                break;
        }
        if (contentType != String.Empty)
        {
            Byte[] bytes = System.IO.File.ReadAllBytes(filePath);             
            SqlCommand cmd = new SqlCommand("InsertFile",Conn);
            Conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@FileName", SqlDbType.VarChar).Value = fileName;
            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType;
            cmd.Parameters.Add("@Content", SqlDbType.Binary).Value = bytes;
            int record = cmd.ExecuteNonQuery();
            lblMessage.ForeColor = System.Drawing.Color.Green;
            lblMessage.Text = "File Uploaded Successfully";
            Conn.Close();
        }
        else
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "File format not recognised." +
              " Upload Image/Word/PDF/Excel formats";
        }
    }
   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 = fileName;                        string Ext = Path.GetExtension(fileName);
                    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();
        }
        catch (SqlException sqlEx)
        {
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "File not  Downloaded Successfully";
        }
    }

如何从SQL Server中选择数据并将其保存到我的计算机

你能试试这样吗?将数据转换为byte[],然后写入文件。

SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
if (reader.HasRows)
{
reader.Read();
byte[] content = reader["FileContent"] as byte[];
FileStream fs = new FileStream(@"c:'temp'tempfile.txt"
  , FileMode.CreateNew, FileAccess.Write);
fs.Write(content, 0, content.Length);
fs.Flush();
fs.Close();
}