如何从C#windows应用程序的图片框中的SQL Server数据库中检索多个图像?

时间:2017-07-16 18:35:04

标签: 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 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);

这有什么问题?请帮忙。

谢谢!

1 个答案:

答案 0 :(得分:0)

主要问题在于以正确的方式保存图像。使用以下代码为每个图像保存图像在数据库图像字段中,并使用上面提到的代码来检索图像。

   @Override
 public void pointTo(Location location) {
        if(mGoogleMap!=null){
        Marker currentLocationMarker = mGoogleMap
                  .addMarker(new MarkerOptions()
                  .position(new 
                   LatLng(location.getLatitude(),location.getLongitude()))
                 .icon(BitmapDescriptorFactory
                 .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
     }
 }
     else{
       latitude=location.latitude;//or whatever property is called
       longitude=location.longitude;;//or whatever property is called
      }
 }

谢谢!

相关问题