[英]How do I upload images to the SQLite database, via a dialog box, and output the image to XAML?
private void Button_Foto_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();// создаем диалоговое окно
openFile.ShowDialog();// открываем окно
string FileName = openFile.FileName;// берем полный адрес картинки
imgPath = openFile.FileName; // записываем в переменную путь к изображению
textBoxImg.Text = FileName; // выводим в текстовом поле путь к изображению (для наглядности)
//InitVariable();
if (textBoxImg.Text == "") // если не указан путь к изображению, то...
return; // прекратить выполнение
// Конвертируем изображение в байты byte[]
byte[] _imageBytes = null;
FileInfo _imgInfo = new FileInfo(imgPath); // загрузим изображение
long _numBytes = _imgInfo.Length; // вычислим длину
FileStream _fileStream = new FileStream(imgPath, FileMode.Open, FileAccess.Read); // откроем изображение на чтение
BinaryReader _binReader = new BinaryReader(_fileStream);
_imageBytes = _binReader.ReadBytes((int)_numBytes); // изображение в байтах
imgFormat = Path.GetExtension(imgPath).Replace(".", "").ToLower(); // запишем в переменную расширение изображения в нижнем регистре, не забыв удалить (Replace) точку перед расширением
imgName = Path.GetFileName(openFile.FileName).Replace(Path.GetExtension(imgPath), ""); // запишем в переменную имя файла, не забыв удалить (Replace) расширение с точкой
// записываем информацию в базу данных
using (SQLiteConnection Connect = new SQLiteConnection("Data Source=" + dbPath + ";Version=3;"))
{
string commandText = "INSERT INTO " + db.AppStaffs + " ([image], [image_format], [image_name]) VALUES(@image, @format, @name)";
SQLiteCommand Command = new SQLiteCommand(commandText, Connect);
Command.Parameters.AddWithValue("@image", _imageBytes);
Command.Parameters.AddWithValue("@format", imgFormat);
Command.Parameters.AddWithValue("@name", imgName);
MessageBox.Show("Изображение добавлено в базу данных");
}
}
At the moment I am using the code to upload the image to the database, but it does not load, it loads by clicking on the button and selecting from the dialog box, I select, upload, gives a message that everything is fine, is it possible to save the image without splitting into arrays and is it possible to make the code easier without splitting into arrays?目前我正在使用代码将图像上传到数据库,但它没有加载,它通过单击按钮并从对话框中选择加载,我选择,上传,给出一条消息,一切都很好,是可以在不拆分为数组的情况下保存图像,是否可以在不拆分为数组的情况下使代码更容易? Both output and load using a single variable
输出和负载都使用单个变量
You are not using executenonquery
method.您没有使用
executenonquery
方法。 so the queries you wrote will not work.所以你写的查询将不起作用。 The message appears after you have completed your work.
完成工作后会出现该消息。
Command.ExecuteNonQuery();
Use this code it will work.使用此代码它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.