C# 如何通过web服务从sql下载二进制(.txt文件)数据

C# 如何通过web服务从sql下载二进制(.txt文件)数据,c#,sql,service,web,C#,Sql,Service,Web,我已经成功地上传和下载了一个txt文件通过二进制到本地sql与此代码在这里 byte[] FileData = File.ReadAllBytes(txtUpload.Text); SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False"); con.Ope

我已经成功地上传和下载了一个txt文件通过二进制到本地sql与此代码在这里

byte[] FileData = File.ReadAllBytes(txtUpload.Text);

            SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
            con.Open();

            SqlCommand cmd = new SqlCommand(@"INSERT into upload (Path,Data,Username,Email,Price,Description)values(@path,@data,@username,@email,@price,@description)", con);
            cmd.Parameters.Add(new SqlParameter("@path", (object)txtUpload.Text));
            cmd.Parameters.Add(new SqlParameter("@data", (object)FileData));
            cmd.Parameters.Add(new SqlParameter("@username", (object)txtSellUsername.Text));
            cmd.Parameters.Add(new SqlParameter("@email", (object)txtSellEmail.Text));
            cmd.Parameters.Add(new SqlParameter("@price", (object)txtSellPrice.Text));
            cmd.Parameters.Add(new SqlParameter("@description", (object)txtSellDescription.Text));


            cmd.ExecuteNonQuery();
            con.Close();
然后像这样下载

FileStream FS = null;
        byte[] dbbyte;

        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM upload where Id='" + txtBuyId.Text + "'", con);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            dbbyte = (byte[])dt.Rows[0]["Data"];

            string filepath = (rootDirectory + "\\Profiles\\" + txtBuyPrice.Text.ToString() + ".txt" );
            FS = new FileStream(filepath, System.IO.FileMode.Create);
            FS.Write(dbbyte, 0, dbbyte.Length);
            FS.Close();
            con.Close();
        }*/
  try
        {
            byte[] FileData = File.ReadAllBytes(txtUpload.Text);
            UserDetailsService.SellProfiles sellprofiles = new UserDetailsService.SellProfiles();

            sellprofiles.Upload = txtUpload.Text;
            sellprofiles.Upload2 = FileData;
            sellprofiles.Username = txtSellUsername.Text;
            sellprofiles.Email = txtSellEmail.Text;
            sellprofiles.Price = txtSellPrice.Text;
            sellprofiles.Name = txtSellProfileName.Text;
            sellprofiles.Description = txtSellDescription.Text;
            obj.InsertSellingProfiles(sellprofiles);

            DialogResult dialogResult = MessageBox.Show("Your profile has been successfully uploaded!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.OK)
            {
                new Menu().Show();
                this.Close();
            }
public byte[] GetFileBytes(strubg someParam)
{
   //read from database and return here
}
从这一点,我已经制定出如何通过这样的网络服务上传

FileStream FS = null;
        byte[] dbbyte;

        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM upload where Id='" + txtBuyId.Text + "'", con);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            dbbyte = (byte[])dt.Rows[0]["Data"];

            string filepath = (rootDirectory + "\\Profiles\\" + txtBuyPrice.Text.ToString() + ".txt" );
            FS = new FileStream(filepath, System.IO.FileMode.Create);
            FS.Write(dbbyte, 0, dbbyte.Length);
            FS.Close();
            con.Close();
        }*/
  try
        {
            byte[] FileData = File.ReadAllBytes(txtUpload.Text);
            UserDetailsService.SellProfiles sellprofiles = new UserDetailsService.SellProfiles();

            sellprofiles.Upload = txtUpload.Text;
            sellprofiles.Upload2 = FileData;
            sellprofiles.Username = txtSellUsername.Text;
            sellprofiles.Email = txtSellEmail.Text;
            sellprofiles.Price = txtSellPrice.Text;
            sellprofiles.Name = txtSellProfileName.Text;
            sellprofiles.Description = txtSellDescription.Text;
            obj.InsertSellingProfiles(sellprofiles);

            DialogResult dialogResult = MessageBox.Show("Your profile has been successfully uploaded!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.OK)
            {
                new Menu().Show();
                this.Close();
            }
public byte[] GetFileBytes(strubg someParam)
{
   //read from database and return here
}
但现在我已经尝试了各种方法从web服务下载二进制数据,但我正在努力寻找解决方案

有人能帮我吗?希望我的代码能帮助更有经验的人帮助我找到解决方案

多谢各位

我尝试从以下服务下载:

  UserDetailsService.Service1Client obj1 = new UserDetailsService.Service1Client();
        byte[] dbbyte;
        FileStream FS;

        UserDetailsService.Service1Client sc = new UserDetailsService.Service1Client();
        if (sc.CheckIfProfileExists(txtBuyId.Text, txtBuyProduct.Text))
        {
            BuyProfile.Id = txtBuyId.Text;
            BuyProfile.Name = txtBuyProduct.Text;

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt = obj1.DownLoadProfile(txtBuyId.Text, txtBuyProduct.Text);
            if (dt.Rows.Count > 0)
            {
                dbbyte = (byte[])dt.Rows[0]["Data"];

                string filepath = (rootDirectory + "\\Profiles\\" + txtBuyProduct.Text.ToString() + ".txt");
                FS = new FileStream(filepath, System.IO.FileMode.Create);
                FS.Write(dbbyte, 0, dbbyte.Length);
                FS.Close();

您可以像这样在Web服务中编写一个方法

FileStream FS = null;
        byte[] dbbyte;

        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM upload where Id='" + txtBuyId.Text + "'", con);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            dbbyte = (byte[])dt.Rows[0]["Data"];

            string filepath = (rootDirectory + "\\Profiles\\" + txtBuyPrice.Text.ToString() + ".txt" );
            FS = new FileStream(filepath, System.IO.FileMode.Create);
            FS.Write(dbbyte, 0, dbbyte.Length);
            FS.Close();
            con.Close();
        }*/
  try
        {
            byte[] FileData = File.ReadAllBytes(txtUpload.Text);
            UserDetailsService.SellProfiles sellprofiles = new UserDetailsService.SellProfiles();

            sellprofiles.Upload = txtUpload.Text;
            sellprofiles.Upload2 = FileData;
            sellprofiles.Username = txtSellUsername.Text;
            sellprofiles.Email = txtSellEmail.Text;
            sellprofiles.Price = txtSellPrice.Text;
            sellprofiles.Name = txtSellProfileName.Text;
            sellprofiles.Description = txtSellDescription.Text;
            obj.InsertSellingProfiles(sellprofiles);

            DialogResult dialogResult = MessageBox.Show("Your profile has been successfully uploaded!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.OK)
            {
                new Menu().Show();
                this.Close();
            }
public byte[] GetFileBytes(strubg someParam)
{
   //read from database and return here
}

您可以从Web服务返回字节[]。你为什么不这样做,返回字节?因此,在服务中,标准的“选择所有匹配id等”将信息绘制到程序中。。然后返回“data”(byte)hmmyes,请看下面我的答案。我会试一试,看看结果如何。我会让你知道的。感谢您的帮助检查顶部的新代码,我尝试了此操作,但没有收到任何异常?顺便问一下,为什么要返回datatable?您可以直接返回byte[],正如我在回答中提到的,我们试图将字节文件存储到dt中,然后从dt下载。对不起,我是个编程新手,编程还不到一年。我完全搞不懂直接从你的方法返回一个字节[]。