我是参考的其他程序,只是原来的程序存储的是路径,不是二进制。一个EmployeeData的数据库,其中Employee的表,DataSetEmployee
主要两个按钮事件
原来的程序如下://选择照片按钮事件
private void btnPicture_Click(object sender, System.EventArgs e)
{
int position=this.BindingContext[this.DataSetEmployy,"Employee"].Position;
this.openFileDialog1.InitialDirectory="c:\\Picture";
this.openFileDialog1.Filter="jpg files(*.jpg)|*.jpg|gif files(*.gif)|*.gif|bmp files(*.bmp)|*.bmp|All files (*.*)|*.*";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.pictureBox1.Image=Image.FromFile(this.openFileDialog1.FileName);
this.DataSetEmployy.Tables["Employee"].Rows[position]["Picture"]=this.openFileDialog1.FileName;
} }//确定添加按钮事件
private void btnApply_Click(object sender, System.EventArgs e)
{
try
{
this.BindingContext[this.DataSetEmployy,"Employee"].EndCurrentEdit();
if(this.Connection1.State==ConnectionState.Closed)
this.Connection1.Open();
OleDbCommandBuilder commandbuilder1=new OleDbCommandBuilder(this.dataAdapter1);
this.dataAdapter1.Update(this.DataSetEmployy,"Employee");
this.DataSetEmployy.AcceptChanges();
this.dataGrid1.Refresh();
}
catch(Exception E)
{
this.ErrorHandle(E);
}
finally
{
this.Connection1.Close();
this.Buttons_Control(false);
}

}
现在确定按钮应该不用更改,我想就是把选择照片的按钮事件的代码更改一下,可是不对。
主要就是这句:
this.DataSetEmployy.Tables["Employee"].Rows[position]["Picture"]=this.openFileDialog1.FileName;
,这句存储的是照片的路径,我想改成存储二进制图片,Employee表中有Picture字段(image类型)
求指点。

解决方案 »

  1.   

    上传二进制
    if (FileUpLogo.HasFile)
    {
      //取得上传文件的大小
      int FileLen = FileUpLogo.PostedFile.ContentLength;
      Byte[] FileData = new Byte[FileLen];
      //创建访问客户端上传文件的对象
      HttpPostedFile hp = FileUpLogo.PostedFile;
      //创建数据流对象
      System.IO.Stream sr = hp.InputStream;
      //将图片数据放到FileData数组对象实例中,0代表数组指针的起始位置,FileLen代表指针的结束位置
      sr.Read(FileData, 0, FileLen);
      //将FileData 赋值给实体
      brandModel.fld_logo = FileData;
    }
    或者HttpPostedFile upFile = up_file.PostedFile;//HttpPostedFile对象,用来读取上传图片的属性
                fileLength = upFile.ContentLength;//记录文件的长度
       try
       {
        if(fileLength==0)//当文件长度为0的时候
        {
         txtMessage.Text = "请选择要上传的文件!";
        }
        else
        {
         byte[] fileByte = new byte[fileLength];//用图片的长度来初始化一个字节数组存储临时的图片文件
         Stream fileStream = upFile.InputStream;//建立文件流对象
         fileStream.Read(fileByte,0,fileLength);//读取图片数据到临时存储体fileByte,0为数据指针位置,fileLength为数据长度
         string connString = "Data Source=192.168.1.250;database=image;uid=pwqzc;pwd=cn0088";
         SqlConnection conn = new SqlConnection(connString);//初始化数据库连接
         string insertStr = "insert into image (image_data,image_content_type,image_description,image_size) values (@image_data,@image_content_type,@image_description,@image_size)";
         //插入数据库语句
         SqlCommand comm = new SqlCommand(insertStr,conn);
         comm.Parameters.Add(new SqlParameter("@image_data",SqlDbType.Image));//添加参数
         comm.Parameters["@image_data"].Value = fileByte;//给参数赋值
         comm.Parameters.Add(new SqlParameter("@image_content_type",SqlDbType.VarChar,50));
         comm.Parameters["@image_content_type"].Value = upFile.ContentType;//记录图片类型
         comm.Parameters.Add(new SqlParameter("@image_description",SqlDbType.VarChar,50));
         comm.Parameters["@image_description"].Value = txtDescription.Text;//把其他的表单数据上传
         comm.Parameters.Add(new SqlParameter("@image_size",SqlDbType.Int,4));
         comm.Parameters["@image_size"].Value = upFile.ContentLength;//记录图片长度,读取数据的时候使用
         conn.Open();//打开数据库连接
         comm.ExecuteNonQuery();//添加数据
         conn.Close();//关闭数据库
         txtMessage.Text = "你已经成功的上传了图片";
        }
       }
       catch(Exception ex)
       {
           txtMessage.Text = ex.Message.ToString();
       }
      }
     }读取
    using(SqlConnection conn=new SqlConnection())  
    {  
    conn.ConnectionString="";  
       
    string strSql="select * from Tb where Id='"+Id+"'";  
    SqlCommand cmd=new SqlCommand(strSql,conn) ;  
    conn.Open();  
    SqlDataReader reader=cmd.ExecuteReader();  
       
    if(reader.Read())  
    {  
    Response.ContentType = "application/octet-stream";  
    Response.BinaryWrite((Byte[])reader["Photo"]);  
    }  
    Response.End();  
    conn.Close();  
      

  2.   

    this.DataSetEmployy.Tables["Employee"].Rows[position]["Picture"]= File.ReadAllBytes(this.openFileDialog1.FileName);
    改成读出流就行。
      

  3.   

    定义数据类型为image型(或其它支持二进制的类型)
    把图片转化 成二进制流。
    用参数方式写入数据库。
      

  4.   


    fangxinggood,我试了,不行啊,提示说:在位置 6 处没有任何行。
    不知道怎么回事啊。用了if判断还是这样。