【发布时间】:2021-11-21 21:28:16
【问题描述】:
我正在尝试构建一个可以将图像插入本地数据库并将它们加载到 flowLayoutPanel 的表单。这是我的表单布局,我在“管理”选项卡中插入图像(图 1)。
在“浏览照片”选项卡中,我尝试通过单击按钮(图 2)将特定城市的照片加载到 flowLayoutPanel。
Load images according to button pressed
我顺利插入照片,但尝试加载时出现问题。
这是我的代码:
public Frm_MyAlbum()
{
InitializeComponent();
}
private void loadImage (int id, FlowLayoutPanel flp)
{
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = Settings.Default.Database1ConnectionString;
SqlCommand command = new SqlCommand();
command.CommandText = $"Select * from Photos where ID = {id}";
command.Connection = conn;
conn.Open();
SqlDataReader DR = command.ExecuteReader();
this.flowLayoutPanel1.Controls.Clear();
while (DR.Read())
{
byte[] bytes = (byte[])DR["Image"];
MemoryStream MS = new MemoryStream(bytes);
PictureBox pics;
pics = new PictureBox();
pics.Image = Image.FromStream(MS);
pics.Size = new Size(200, 160);
pics.SizeMode = PictureBoxSizeMode.StretchImage;
this.flowLayoutPanel1.Controls.Add(pics);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void GetID(string City, FlowLayoutPanel flp)
{
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = Settings.Default.Database1ConnectionString;
conn.Open();
SqlCommand command = new SqlCommand();
command.CommandText = $"Select * from Photos where City = {City}";
command.Connection = conn;
SqlDataReader DR = command.ExecuteReader();
while (DR.Read())
{
loadImage((int)DR["PhotoID"], flp);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e) //button London
{
this.flowLayoutPanel1.Controls.Clear();
GetID("London", flowLayoutPanel1);
}
private void button7_Click(object sender, EventArgs e) //button Add to DB
{
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = Settings.Default.Database1ConnectionString;
SqlCommand command = new SqlCommand();
command.CommandText = $"Insert into Photos(City, Description, Image) values(@City, @Desc, @Image)";
command.Connection = conn;
byte[] bytes;
MemoryStream MS = new MemoryStream();
this.pictureBox1.Image.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg);
bytes = MS.GetBuffer();
command.Parameters.Add("@City", SqlDbType.Text).Value = this.textBox2.Text;
command.Parameters.Add("@Desc", SqlDbType.Text).Value = this.textBox1.Text;
command.Parameters.Add("@Image", SqlDbType.Image).Value = bytes;
conn.Open();
command.ExecuteNonQuery();
MessageBox.Show("Adding Successfully");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button8_Click(object sender, EventArgs e) //button browse
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
当我运行代码时,我按下了 London(按钮 1)并且异常显示为“无效的列名 'London'”。 VS 表示 'command.CommandText = $"Select * from Photos where City = {City}";' 这一行有问题。
尝试了很多方法来重写它,但没有弄明白。 我应该怎么做才能解决这个问题?
提前谢谢你们!!
【问题讨论】:
-
首先您将直接字符串传递到您的 sql 中(始终使用参数)。无论如何尝试 City = '{City}'
-
OK 稍后会尝试参数!我确实尝试使用'{City}',但弹出另一个异常“数据类型 text 和 varchar 在等于运算符中不兼容”
-
Imagedb 格式应该替换成varbinary(max)-- 这个:bytes = MS.GetBuffer();非常错误,你需要[MemoryStream].ToArray(),而不是GetBuffer()-- 强制重新格式化图像从任何东西到JPEG都是不好的。 -- 最后,这个:this.flowLayoutPanel1.Controls.Clear();可能是您在 WinForms 应用程序中可以做的最糟糕的事情之一。但是你还有其他突出的问题。 -
谢谢 Jimi,我是编程新手,非常感谢您的意见。查一下你提到的几点。
标签: c# database winforms sqlcommand flowlayoutpanel