C# 如何在bot框架下保存sqlserver中的文件附件
我已经添加了sql代码。当我从数据中删除image变量时,sql语句成功执行,然后我知道image数据有问题C# 如何在bot框架下保存sqlserver中的文件附件,c#,botframework,C#,Botframework,我已经添加了sql代码。当我从数据中删除image变量时,sql语句成功执行,然后我知道image数据有问题 var url = state.Image.Attachment.ContentUrl; var httpClient = new HttpClient(); var imageData = await httpClient.GetByteArrayAsync(url); string constr = ConfigurationMan
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.
我将图像转换为Base64。imageData变量的内容大小是多少?添加用于将数据保存到数据库中的代码。我添加了sql语句,并进一步解释了图像大小小于100MB您不能(也不应该)仅在sql查询中保存要保存的值。请使用预先准备好的语句,如有可能,请参阅
图像大小小于100mb
Hi@oluwatosinessamuelbabalola,您可以尝试将附件文件存储在Azure存储中,并将附件文件的URL保存在SQL数据库中,而不是直接将大文件存储在数据库中。我应该安装什么名称空间来访问StorageFile?System.Windows.storage,System.Windowsi下载了该软件包,但在尝试将其添加到“我的项目”中的引用时出错。有几个不同的版本应适合您的操作系统。你会找到其他你会找到的网站。
var url = state.Image.Attachment.ContentUrl;
var httpClient = new HttpClient();
StorageFile f1 = await httpClient.GetByteArrayAsync(url);
string imageData = Convert.ToBase64String(File.ReadAllBytes(f1.Path));