如何通过Web服务从sql下载二进制(.text文件)数据

时间:2013-08-29 08:29:44

标签: 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.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();
            }

但是现在我尝试了各种方法从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();

1 个答案:

答案 0 :(得分:0)

你可以像这样在你的网络服务中编写一个方法

public byte[] GetFileBytes(strubg someParam)
{
   //read from database and return here
}
相关问题