我正在开发一个用于图像处理的可视化 C# 程序。我正在尝试使用 Visual C#(Windows 窗体)和 ADO.NET 将图像添加到 sql 数据库。
我已使用文件流方法将图像转换为二进制形式,但图像字节未保存在数据库中。在数据库图像列,它显示 < 二进制数据 > 并且没有数据被保存!
我尝试了很多插入方法(有和没有存储过程......等),但总是在数据库中得到同样的东西。
private void button6_Click(object sender, EventArgs e)
{
try
{
byte[] image = null;
pictureBox2.ImageLocation = textBox1.Text;
string filepath = textBox1.Text;
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
image = br.ReadBytes((int)fs.Length);
string sql = " INSERT INTO ImageTable(Image) VALUES(@Imgg)";
if (con.State != ConnectionState.Open)
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(new SqlParameter("@Imgg", image));
int x= cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(x.ToString() + "Image …Run Code Online (Sandbox Code Playgroud)