如何在Bot框架中的SQL Server中保存文件附件

时间:2018-07-21 08:03:28

标签: c# botframework

        var url = state.Image.Attachment.ContentUrl;
        var httpClient = new HttpClient();
        var imageData = await httpClient.GetByteArrayAsync(url);

 string constr = ConfigurationManager.ConnectionStrings["ASPNETConnectionString"].ToString();
                    SqlConnection conn = new SqlConnection(constr);

                    string sql = "INSERT into dbo.mee (name,image,status,guid) VALUES ('" + name + "','" + imageData + "','" + status + "','" + g + "');";
                    SqlCommand cmd = new SqlCommand(sql);
                    cmd.Connection = conn;
                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        await context.PostAsync($"Your details has been received");
                    }
                    catch (Exception ex)
                    {
                        await context.PostAsync($"cannot connect to database");

                    }
                    finally
                    {
                        conn.Close();
                    }
                 };

    I want to save the image attachment uploaded by the user to sql server database. I used the imageData variable in my sql statement but the image would not save. I have already used varbinary(max) as the data type in my sql table. is there anything wrong with the code above? please i need help. 

我已经添加了sql代码。当我从数据中删除图像变量时,成功执行了sql语句,那么我知道图像数据出了问题

1 个答案:

答案 0 :(得分:0)

       var url = state.Image.Attachment.ContentUrl;
       var httpClient = new HttpClient();
       StorageFile f1 = await httpClient.GetByteArrayAsync(url);
       string imageData = Convert.ToBase64String(File.ReadAllBytes(f1.Path));

我将图像转换为Base64。