gpt4 book ai didi

c# - 如何将数据插入到 blob C#/mysql/uwp

转载 作者:行者123 更新时间:2023-11-29 19:36:02 26 4
gpt4 key购买 nike

我正在尝试将 PNG 文件上传到 mysql 数据库 blob 类型列。

我已经到处搜索了,这就是我到目前为止所拥有的,但我被困住了......我最终在数据库中得到了 System.byte[]。

public static byte[] ArtworkRawData

StorageFile artworkfile = await openPicker.PickSingleFileAsync();
if (artworkfile != null)
{
artworkSet = true;
//var stream = await musicfile.OpenAsync(Windows.Storage.FileAccessMode.Read);
artworkFileBTN.Content = artworkfile.DisplayName;
var stream = await artworkfile.OpenAsync(FileAccessMode.Read);
var streamBytes = await artworkfile.OpenStreamForReadAsync();
var bytes = new byte[(int)streamBytes.Length];
ArtworkRawData = bytes;
var image = new BitmapImage();
await image.SetSourceAsync(stream);
artworkView.Source = image;
}

我的查询如下所示:

if (DBC.Insert("INSERT INTO music(artwork) values('" +UploadMusicDialog.ArtworkRawData + "')")){

//do some stuff
}

更新

public bool Insert(string query)
{
//open connection
if (OpenConnection() == true)
{
try
{
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
return true;

}
catch
{
return false;
}
finally
{
CloseConnection();
}

}else
{

return false;
}
}

最佳答案

你应该使用这个:

public bool Insert(string query, byte[] rawImage)
{
//open connection
if (OpenConnection() == true)
{
try
{
MySqlCommand cmd = new MySqlCommand(query, connection);

MySqlParameter parameter = new MySqlParameter("@rawImage", MySqlDbType.Blob);
parameter.Value = rawImage;
cmd.Parameters.Add(parameter);

cmd.ExecuteNonQuery();
return true;

}
catch
{
return false;
}
finally
{
CloseConnection();
}

}else
{

return false;
}
}

并这样调用它:

if (DBC.Insert("INSERT INTO music(artwork) values(@rawImage)", UploadMusicDialog.ArtworkRawData))
{

//do some stuff
}

无论如何,像这样对查询进行硬编码并不是一个好主意。它以高度不安全而闻名。

关于c# - 如何将数据插入到 blob C#/mysql/uwp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41553343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com