大家好,我用VS2005编了一个WINFORM程序,主要是想将图片存入到数据库中(数据库为SQL数据库)
自己编写的代码如下:创建一个数据库,用来保存图片
create database MyDb
use MyDb
create table MyPhoto
( id int primary key,
photo image //图片使用image 类型 ) 然后,创建一个Windows应用程序,添加一个openFileDialog,一个 pictureBox,一个button(button 1的Text属性改为"上传")。 由于需要对文件操作,引入System.IO命名空间。对数据库操作当然要引入System.Data.SqlClient啦。基本思路就是将图片转化为字节数组保存起来,这时需要BinaryReader读取二进制字节。 代码部分:
—————————————————————————————————— using System.IO;
using System.Data.SqlClient; namespace 如何将图片存入数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//获取文件对话框中选定的文件名的字符串,包括文件路径
pictureBox1.ImageLocation = openDialog1.FileName;
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];//fs.Length文件流的长度,用字节表示
BinaryReader br = new BinaryReader(fs);//二进制文件读取器
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//从当前流中将count个字节读入字节数组中
SqlConnection conn = new SqlConnection("server=(local);initial catalog=MyDb;integrated security=true");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into MyPhoto values(@id,@Image)", conn);
cmd.Parameters.Add("@id", SqlDbType.Int, 4);
cmd.Parameters.Add("@Image", SqlDbType.Image);
cmd.Parameters["@id"].Value = 1;
cmd.Parameters["@Image"].Value = imagebytes;
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("图片上传成功");
}
} }
} 该代码只能上传一张图片,若要在上传,会在cmd.ExecuteNonQuery(); 处出现异常。想请大家帮忙看看,怎样能多次上传。也乐意接受大家新写的代码(只要能实现预期功能即可)。
自己编写的代码如下:创建一个数据库,用来保存图片
create database MyDb
use MyDb
create table MyPhoto
( id int primary key,
photo image //图片使用image 类型 ) 然后,创建一个Windows应用程序,添加一个openFileDialog,一个 pictureBox,一个button(button 1的Text属性改为"上传")。 由于需要对文件操作,引入System.IO命名空间。对数据库操作当然要引入System.Data.SqlClient啦。基本思路就是将图片转化为字节数组保存起来,这时需要BinaryReader读取二进制字节。 代码部分:
—————————————————————————————————— using System.IO;
using System.Data.SqlClient; namespace 如何将图片存入数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//获取文件对话框中选定的文件名的字符串,包括文件路径
pictureBox1.ImageLocation = openDialog1.FileName;
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];//fs.Length文件流的长度,用字节表示
BinaryReader br = new BinaryReader(fs);//二进制文件读取器
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//从当前流中将count个字节读入字节数组中
SqlConnection conn = new SqlConnection("server=(local);initial catalog=MyDb;integrated security=true");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into MyPhoto values(@id,@Image)", conn);
cmd.Parameters.Add("@id", SqlDbType.Int, 4);
cmd.Parameters.Add("@Image", SqlDbType.Image);
cmd.Parameters["@id"].Value = 1;
cmd.Parameters["@Image"].Value = imagebytes;
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("图片上传成功");
}
} }
} 该代码只能上传一张图片,若要在上传,会在cmd.ExecuteNonQuery(); 处出现异常。想请大家帮忙看看,怎样能多次上传。也乐意接受大家新写的代码(只要能实现预期功能即可)。
解决方案 »
- Socket连接过去,收不到数据过来为什么哦,给位大大给看看
- 问题又来了
- 计算量大,未响应问题,如何解决?(C# winform)
- treeview连接到数据库有问题?请帮看看
- <求救>才学C#,请问如何用程序判断抓到网页是gb2312/utf-8???
- 怎样在WPF运动的3D物体表面上贴上2D内容?
- C#中如何定义含有事件的变量?
- 如何让文本框里显示HTML
- 请教一个有关codesoft打印的问题
- c# 一个简单的窗体上有一个按钮,想用另外的程序控制这个按钮 FindWindow()
- 鼠标悬停在TreeView节点上就出现节点信息,想取消息这个节点信息,请前辈指点!小弟选感谢了!
- 调用com组件的方法出错
这个id已经存在了。设置id字段为自动增长。然后不要指定id。
就可不使用id
SqlCommand cmd = new SqlCommand("insert into 表名 ( 列名 ) values(@Image)", conn);
cmd.Parameters.Add("@Image", SqlDbType.Image).Value = imagebytes;
企业管理器 --- 数据表 --- 设计表 --- 数据类型 --- 选int 或Bigint -- 标识 --选择"是"
using System.IO;
using System.Data.SqlClient; namespace 如何将图片存入数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "*jpg|*.jpg|*bmp|*.bmp|*gif|*.gif";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//获取文件对话框中选定的文件名的字符串,包括文件路径
pictureBox1.ImageLocation = openDialog1.FileName;
FileStream fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] imagebytes = new byte[fs.Length];//fs.Length文件流的长度,用字节表示
BinaryReader br = new BinaryReader(fs);//二进制文件读取器
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));//从当前流中将count个字节读入字节数组中
SqlConnection conn = new SqlConnection("server=(local);initial catalog=MyDb;integrated security=true");
conn.Open();
SqlCommand cmd = new SqlCommand("insert into MyPhoto values(@id,@Image)", conn); cmd.Parameters.Add("@Image", SqlDbType.Image); cmd.Parameters["@Image"].Value = imagebytes; conn.Close();
MessageBox.Show("图片上传成功");
}
} }
}
可以运行,但是返回数据库看的时候,并没有把图像添加进去,id和image显示都是NULL
不知道代码哪里有问题,请大家帮忙看看,十分感谢。
add constraint pk_为主键取的名字 primary key (列名)这是TSQ 代码
如果用手动添加主键 就是选择你要添加主键的的列 点击右键选择主键就OK拉