Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从SQL Server中选择数据并将其保存到我的计算机_C#_Sql Server_Windows Applications - Fatal编程技术网

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

C# 如何从SQL Server中选择数据并将其保存到我的计算机,c#,sql-server,windows-applications,C#,Sql Server,Windows Applications,我从数据库中按名称选择了二进制数据,然后我显示了一个SaveFileDialog,将二进制数据保存在我计算机上的某个位置。 我写了下面的代码来实现这一点,但是当我打开保存的文件时,文件中没有任何数据。它只适用于txt文件。我怎样才能解决这个问题 private void btnUpload_Click(object sender, EventArgs e) { fdTransfer.ShowDialog(); string filePath = fdTr

我从数据库中按名称选择了二进制数据,然后我显示了一个
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";
        }

    }

你能试试这样的吗?将数据转换为字节[],然后将其写入文件

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();
}

您确定
sdr[“Content”]
包含数据吗?您也从不关闭/刷新流。是的,在我的表的内容列中有。我应该在哪里关闭流?我编写了sw.close(),但当我打开保存的文件时,出现错误:无法打开文件,因为内容存在问题
SqlDataRaeader
StreamWriter
是否关闭?请对
SqlCommand
SqlDataReader
StreamWriter
使用
using
语句。如果您在数据库中存储二进制数据,而不是传递二进制参数而不是varchar,请将sqlDatatype.binary设为sqlDatatype并查看它是否工作。当我打开保存的文件时,也有同样的错误内容字节数组的大小是多少?您试图打开的文件类型是什么?为了测试,我定义了二进制(1000),然后上传并下载了一个具有2个字符二进制(1000)的docx-我怀疑您是否能将docx文件放入1000字节。