大家好,我用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(); 处出现异常。想请大家帮忙看看,怎样能多次上传。也乐意接受大家新写的代码(只要能实现预期功能即可)。

解决方案 »

  1.   

    cmd.Parameters["@id"].Value = 1; 
    这个id已经存在了。设置id字段为自动增长。然后不要指定id。
      

  2.   

    提示什么错误,id是否为主键,是设置为自增
    就可不使用id
      

  3.   

    直接DESIGN TABLE  選擇列 identity
      

  4.   

    问题一:有楼上提到的id重复, insert into 语句也有问题. 数据表名 和 列名 都不能少
    SqlCommand cmd = new SqlCommand("insert into 表名 ( 列名 ) values(@Image)", conn);
    cmd.Parameters.Add("@Image", SqlDbType.Image).Value = imagebytes;  
      

  5.   


    企业管理器 --- 数据表 --- 设计表 --- 数据类型 --- 选int 或Bigint  -- 标识 --选择"是"
      

  6.   

    呵呵 刚才弄清楚怎样设置自增主键了 并将代码改写如下:
    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
    不知道代码哪里有问题,请大家帮忙看看,十分感谢。
      

  7.   

    已经在7楼告诉你了,怎么还是这样写 SQL语句表名是什么? 列名是什么?
      

  8.   

    alter table 表名
    add constraint pk_为主键取的名字 primary key (列名)这是TSQ 代码
         如果用手动添加主键 就是选择你要添加主键的的列 点击右键选择主键就OK拉