从 SQL Server 数据库检索图像

发布于 2024-11-26 03:19:32 字数 697 浏览 1 评论 0原文

我有一个问题,当我从内存流中的数据库检索图像时,它给出错误参数无效。请帮助我解决这个问题。

代码:

private void button3_Click(object sender, EventArgs e)
{
   string strcon = "Data Source=PINKAL-PC; initial catalog=testing; integrated security=SSPI;";

   SqlConnection sqlcon = new SqlConnection(strcon);
   sqlcon.Open();

   string strquery = "select * from testimg";

   SqlDataAdapter da = new SqlDataAdapter(strquery,sqlcon);
   DataSet ds = new DataSet();
   da.Fill(ds);

   DataTable dt = new DataTable();
   dt = ds.Tables[0];

   byte[] barrImg = (byte[])dt.Rows[7]["image"];
   MemoryStream mstream = new MemoryStream(barrImg);

   pictureBox2.Image = Image.FromStream(mstream);
}

I have a question that when I retrieve image from database from memory stream it gives an error Parameter is not valid. Please help me out from this problem.

Code:

private void button3_Click(object sender, EventArgs e)
{
   string strcon = "Data Source=PINKAL-PC; initial catalog=testing; integrated security=SSPI;";

   SqlConnection sqlcon = new SqlConnection(strcon);
   sqlcon.Open();

   string strquery = "select * from testimg";

   SqlDataAdapter da = new SqlDataAdapter(strquery,sqlcon);
   DataSet ds = new DataSet();
   da.Fill(ds);

   DataTable dt = new DataTable();
   dt = ds.Tables[0];

   byte[] barrImg = (byte[])dt.Rows[7]["image"];
   MemoryStream mstream = new MemoryStream(barrImg);

   pictureBox2.Image = Image.FromStream(mstream);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在看孤独的风景 2024-12-03 03:19:32

不确定问题的实际根本原因是什么 - 我的猜测是数据库表 testimg 中没有名为 image 的列,但是您尝试阅读该专栏以获取您的图片。

但我一般会为您的代码推荐以下几点:

private void button3_Click(object sender, EventArgs e)
{
   string strcon = "Data Source=PINKAL-PC; initial catalog=testing; integrated security=SSPI;";

   // Put your SqlConnection into using blocks to ensure proper disposal
   using(SqlConnection sqlcon = new SqlConnection(strcon))
   { 
     //  sqlcon.Open();  -- don't open it here already - open as LATE as possible....
     // for SqlDataAdapter - you don't even need to open it yourself - 
     // the data adapter will do this automatically for you
     // and **IF** you open it yourself - you also need to CLOSE it again!

     // *NEVER* use SELECT * in code !! specify the columns you want explicitly
     // string strquery = "select * from testimg";
     string strquery = "SELECT col1, col2, col3 ..... FROM dbo.testimg";  

     SqlDataAdapter da = new SqlDataAdapter(strquery, sqlcon);

     //DataSet ds = new DataSet();  if you only want a single DataTable - no point in having a  whole DataSet ! That's just overhead.....
     //da.Fill(ds);
     //DataTable dt = new DataTable();
     //dt = ds.Tables[0];
     DataTable dt = new DataTable();
     da.Fill(dt);

     // is there a "image" column in your table?? 
     // You need to use the proper column name here!
     byte[] barrImg = (byte[])dt.Rows[7]["image"];
     MemoryStream mstream = new MemoryStream(barrImg);

     pictureBox2.Image = Image.FromStream(mstream);
  }
}

Not sure what the actual root cause of your problem is - my guess would be that there is no column by the name of image in your database table testimg, but you're trying to read that column to get your picture.

But here are a few things I'd recommend for your code in general:

private void button3_Click(object sender, EventArgs e)
{
   string strcon = "Data Source=PINKAL-PC; initial catalog=testing; integrated security=SSPI;";

   // Put your SqlConnection into using blocks to ensure proper disposal
   using(SqlConnection sqlcon = new SqlConnection(strcon))
   { 
     //  sqlcon.Open();  -- don't open it here already - open as LATE as possible....
     // for SqlDataAdapter - you don't even need to open it yourself - 
     // the data adapter will do this automatically for you
     // and **IF** you open it yourself - you also need to CLOSE it again!

     // *NEVER* use SELECT * in code !! specify the columns you want explicitly
     // string strquery = "select * from testimg";
     string strquery = "SELECT col1, col2, col3 ..... FROM dbo.testimg";  

     SqlDataAdapter da = new SqlDataAdapter(strquery, sqlcon);

     //DataSet ds = new DataSet();  if you only want a single DataTable - no point in having a  whole DataSet ! That's just overhead.....
     //da.Fill(ds);
     //DataTable dt = new DataTable();
     //dt = ds.Tables[0];
     DataTable dt = new DataTable();
     da.Fill(dt);

     // is there a "image" column in your table?? 
     // You need to use the proper column name here!
     byte[] barrImg = (byte[])dt.Rows[7]["image"];
     MemoryStream mstream = new MemoryStream(barrImg);

     pictureBox2.Image = Image.FromStream(mstream);
  }
}
瑕疵 2024-12-03 03:19:32

如果您的错误与将图像从数据库转换为流有关,请尝试像这样修改您的代码:

 byte[] barrImg = (byte[])dt.Rows[7]["image"];

 MemoryStream mstream = new MemoryStream();

 mstream .Write ( barrImg, 0, barrImg.Length );       
 mstream .Seek ( 0, SeekOrigin.Begin );        
 mstream .Close();

 pictureBox2.Image = Image.FromStream(mstream);

If your error is concerned with converting the image from database into stream, Just try to modify your code like this::

 byte[] barrImg = (byte[])dt.Rows[7]["image"];

 MemoryStream mstream = new MemoryStream();

 mstream .Write ( barrImg, 0, barrImg.Length );       
 mstream .Seek ( 0, SeekOrigin.Begin );        
 mstream .Close();

 pictureBox2.Image = Image.FromStream(mstream);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文