C# 从C应用程序在SQL Server上上载和下载文件

C# 从C应用程序在SQL Server上上载和下载文件,c#,sql,sql-server,file,filestream,C#,Sql,Sql Server,File,Filestream,我有一个C应用程序正在将文件上载到sql server,我使用此代码获取pdf文件,然后将其更改为字节以上载到sql server数据库 private void mButtonAddCV_Click(object sender, EventArgs e) { openFileDialog1.Filter = "PDF Files | *.pdf"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogRe

我有一个C应用程序正在将文件上载到sql server,我使用此代码获取pdf文件,然后将其更改为字节以上载到sql server数据库

private void mButtonAddCV_Click(object sender, EventArgs e)
{
    openFileDialog1.Filter = "PDF Files | *.pdf";
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        if (openFileDialog1.FileName.Length > 0)
        {
            pathCV = openFileDialog1.FileName;
        }
    }

    // Read the file and convert it to Byte Array
    string filePath = pathCV;
    string contenttype = String.Empty;

    contenttype = "application/pdf";

    if (contenttype != String.Empty)
    {
        Stream fs = File.OpenRead(filePath);
        BinaryReader br = new BinaryReader(fs);
        bytes = br.ReadBytes((Int32)fs.Length);
    }
}
我使用以下代码上载文件:

if (!mConnector.Update("INSERT INTO **** (***, ***, CV) " +
                            "VALUES ('" + *** + "', '" + *** + "', '" + bytes + "')"))
{
    Messages.errorMessageBox("This CV already exists.");
}
else
{
    ChangeScreen(ActiveScreen, ActiveScreens.ActiveScreen_CVList);
}
但是现在我不知道如何下载这个文件,也不知道如何用存储在数据库中的数据制作一个pdf文件来查看它。有人能帮我吗


谢谢

我建议您使用SqlParameters插入字节数据。。。 看


然后,使用SqlDataReader的GetBytes读取记录。。。函数请参见。

首先,让我们更改生成insert语句的方式,这样就不会向sql注入打开系统。这也将使insert语句更易于使用

var command = new SqlCommand("INSERT INTO myTable (x, y, z) VALUES (@a, @b, @c)", sqlConnection);
command.Parameters.Add(new SqlParameter("@a", bytes));
command.Parameters.Add(new SqlParameter("@b", bValue));
command.Parameters.Add(new SqlParameter("@c", bValue));

var resultingRows = command.ExecuteNonQuery();
要读取数据,请使用ExecuteReader,然后使用File对象将其保存到磁盘

var command = new SqlCommand("Select a from myTable", sqlConnection);
var reader = command.ExecuteReader();
reader.Read();
var pdfBinaryBuffer = (byte[])reader[0];

// Save file to disk
var file = File.Create("myFile.pdf", pdfBinaryBuffer.Length);
file.Write(pdfBinaryBuffer, 0, pdfBinaryBuffer.Length);
file.Close();

我建议你上传pdf并保存在单独的文件夹中。您可以将路径保存在我认为很好的数据库表中

下面是文件上传的代码 拖动“Fileupload”控件到.aspx页面使用此代码是为了将.PDF保存到文件夹

protected void fileUpload()
{
 if (fileUp.HasFile)
            {

                fileUp.SaveAs(Server.MapPath("~/PoPDF/" + this.txtCusPo.Text +".PDF"));
                string imgPrintPo = this.txtCusPo.Text + ".PDF";

            }
}
下面是文件下载的代码 您可以将此代码放在按钮事件中,但在这里我使用了GridView行命令事件

protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
   GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
   if (e.CommandName == "SelectDownload")
   {

      Response.Clear();
      Response.ContentType = "application/octet-stream";
      Response.AppendHeader("Content-Disposition", "filename=" + e.CommandArgument);
      Response.TransmitFile(Server.MapPath("~/PoPDF/") + e.CommandArgument);
        //Response.Flush();
             Response.End();
   }

}

我怀疑你能插入你的字节吗?SQL Server中的字节是一个很长的十六进制值,类似于0x9034BA94F832C….当您尝试连接字符串和字节数组时,一定存在编译时错误。。。。或者,从另一个角度来看,它是在CV列中插入System.Byte[]我插入这个,我不知道这是我的pdf文件还是什么:bytes[760483]:[0]37,[1]80,[2]114,[760483]34据我所知。。。pdf文件以%pdf开头,后跟一个版本号,意思是[0]37、[1]80、[2]68、[3]70……是的,是我的字节,我把其他数字作为随机数,但我的字节有这个值,你说我收到这句话的结果:List lResults=mConnector.QuerySELECT CV FROM***其中DNI='+valueToSearch+',lCols;我能做你所做的一切吗?我有自己的类来连接数据库并对其进行操作,如果可能的话,我更愿意继续这样做。谢谢最好的方法是使用orm,如EF6。这将解决sql注入问题,并允许您一次获取所有行。这将需要一点来设置,但最终,它将为您节省大量时间。我不会让你自己玩。这是一个网络应用程序。他正在为windows桌面开发。