簡體   English   中英

如何從SQL Server選擇數據並將其保存到我的計算機

[英]How to select data from SQL Server and save it to my computer

我已經從數據庫中按其名稱選擇了二進制數據,然后顯示了一個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";
        }

    }

你可以嘗試這樣的事情嗎? 將數據轉換為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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM