| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1093 人关注过本帖
标题:如何在数据库中存入图片
只看楼主 加入收藏
雨晴
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-2-24
收藏
 问题点数:0 回复次数:7 
如何在数据库中存入图片
怎么才能把图片存到数据库中呢????
搜索更多相关主题的帖子: 数据库 
2006-07-20 16:17
yangming
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-7-4
收藏
得分:0 

以二进制流写到数据库里

2006-07-20 16:38
雨晴
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-2-24
收藏
得分:0 
谢谢了,我试一下

曾经失落过,徘徊过,但我仍期待收获!
2006-07-20 16:48
marer
Rank: 2
等 级:新手上路
威 望:3
帖 子:928
专家分:0
注 册:2005-7-18
收藏
得分:0 

给段代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
DataSet ds=new DataSet();
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Left;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 268);
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.textBox1.Location = new System.Drawing.Point(464, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 273);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// panel1
//
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(120, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(344, 208);
this.panel1.TabIndex = 2;
//
// pictureBox1
//
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(344, 208);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "button1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(256, 224);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 23);
this.button2.TabIndex = 4;
this.button2.Text = "button2";
//
// button3
//
this.button3.Location = new System.Drawing.Point(376, 224);
this.button3.Name = "button3";
this.button3.TabIndex = 5;
this.button3.Text = "button3";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("database=Pics;user id=sa;password=sa;data source=(local)");
SqlDataAdapter da=new SqlDataAdapter("select * from pic",con);
da.Fill(ds,"pic");
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
this.listBox1.Items.Add(ds.Tables[0].Rows[i][0]);
}
this.listBox1.SelectedIndex=0;
this.textBox1.DataBindings.Add(new Binding("Text",ds,"pic.Other"));
//showPic();
}

private void showPic()
{
if(this.pictureBox1.Image!=null)
{
this.pictureBox1.Image.Dispose();
}
if(ds.Tables[0].Rows[this.listBox1.SelectedIndex][1]!=Convert.DBNull)
{
byte[] bytes=(byte[])ds.Tables[0].Rows[listBox1.SelectedIndex][1];
MemoryStream ms=new MemoryStream(bytes);
Bitmap im=new Bitmap(ms,false);
this.pictureBox1.Image=im;
}
else
{
this.pictureBox1.Image=null;
}
}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.BindingContext[ds,"pic"].Position=listBox1.SelectedIndex;
showPic();
}
}
}


public class 人生历程 extends Thread{public void run(){while(true){努力,努力,再努力!!;Thread.sleep(0);}}}
2006-07-21 10:38
niat32214
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2006-7-29
收藏
得分:0 
一个更好的方法,把图片的地址存在数据库里

2006-07-29 19:58
那天风很大
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-7-29
收藏
得分:0 

在用了4楼的代码后出现了以下的异常:

未处理的“System.ArgumentException”类型的异常出现在system.drawing.dll中。

其他信息:使用了无效参数

异常位置是:Bitmap im=new Bitmap(ms,false);

请问是怎么回事呢?如何能解决呢?

2006-07-29 23:51
man1234
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-8-5
收藏
得分:0 

这种方法是将图片存储在SQL中,那么如何将图片存在ACCESS数据库中?


2006-08-07 21:01
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
一樣
都是弄為二進制存進去

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-08 08:49
快速回复:如何在数据库中存入图片
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.068824 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved