如何在C#windows应用程序中从picturebox中的SQL Server数据库检索多个图像?
我正在尝试以下c#windows应用程序中的代码,这些代码可以很好地从SQL Server数据库表中检索单个映像如何在C#windows应用程序中从picturebox中的SQL Server数据库检索多个图像?,c#,.net,sql-server,windows-applications,C#,.net,Sql Server,Windows Applications,我正在尝试以下c#windows应用程序中的代码,这些代码可以很好地从SQL Server数据库表中检索单个映像 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using S
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
namespace ManagementSystem
{
public partial class frmSearchResult : Form
{
public frmSearchResult()
{
InitializeComponent();
}
SqlConnection con;
SqlCommand cmd;
private void cmdSearch_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=managementsystem;Integrated Security=True");
con.Open();
cmd = new SqlCommand("Select M_Photo, S_Photo From Members where M_Number=15", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["M_Photo"]);
pic_M.Image = new Bitmap(ms);
}
}
}
}
但我需要从数据库中检索多个图像。当我在末尾附加以下代码时,我得到一个错误:
参数无效
附加的代码是
MemoryStream ms1 = new MemoryStream((byte[])ds.Tables[0].Rows[0]["S_Photo"]);
pic_S.Image = new Bitmap(ms1);
这有什么问题?请帮忙
谢谢 主要问题在于以正确的方式保存图像。对每个图像使用下面的代码将图像保存在数据库图像字段中,并使用上面的代码检索图像
//Convert image to binary
string imgpathC2 = txtH_C2.Text;
FileStream fsC2;
fsC2 = new FileStream(@imgpathC2, FileMode.Open, FileAccess.Read);
byte[] picbyteC2 = new byte[fsC2.Length];
fsC2.Read(picbyteC2, 0, System.Convert.ToInt32(fsC2.Length));
fsC2.Close();
//Add binary value to SQL parameter
SqlParameter picparaC2 = new SqlParameter();
picparaC2.SqlDbType = SqlDbType.Image;
picparaC2.ParameterName = "picC2";
picparaC2.Value = picbyteC2;
//use parameter in command
cmd.Parameters.Add(picparaC2);
谢谢 不。。。永远不要在SQL中保存图像!请告诉我,保存和检索图像@proxytype的最佳方法是什么?使用某种blob或文件存储图像,并在数据库中保留对图像的引用。将图像保存在表中会使数据库膨胀。不要将二进制文件保存在sql中,只需直接链接到文件系统……有人能为我提供使用文件系统存储和检索图像的链接或代码吗?