private void pictureBox1_DoubleClick(object sender, System.EventArgs e)
{
try
{
this.openFileDialog1.Filter = "BMP 图片 (*.bmp)|*.bmp|JPG 图片 (*.jpg)|*.jpg|GIF 图片 (*.gif)|*.gif|JPEG 图片(*.jpeg)|*.jpeg\" ";
openFileDialog1.Title = "请选择图片";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.pictureBox1.Image=Image.FromFile(this.openFileDialog1.FileName);

}
FileStream fs=new FileStream(this.openFileDialog1.FileName,FileMode.OpenOrCreate,FileAccess.Read);
theData=new byte[fs.Length];
fs.Read(theData,0,System.Convert.ToInt32(fs.Length));
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
////上面为读取图片,下面保存
sqlcon.Open();
DataSet ds=new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select top 10 * From Product",sqlcon);
SqlCommandBuilder MyCB = new SqlCommandBuilder(da);
/* da.SelectCommand.CommandText="select * from Product";
da.SelectCommand.Connection=sqlcon;
da.InsertCommand.Connection=sqlcon;
SqlCommand sqlcom=new SqlCommand("Insert into Product(Productdh,Productlb,sccs,Productjj,Particular,Pic) values (@Productdh,@Productlb,@sccs,@Productjj,@Particular,@Pic)",sqlcon);
//sqlcom.CommandType=CommandType.StoredProcedure;
sqlcom.Parameters.Add("@Productdh",SqlDbType.VarChar,13);
sqlcom.Parameters["@Productdh"].Value=productdh;
sqlcom.Parameters.Add("@Productlb",SqlDbType.VarChar,10);
sqlcom.Parameters["@Productlb"].Value=e1;
sqlcom.Parameters.Add("@sccs",SqlDbType.VarChar,30);
sqlcom.Parameters["@sccs"].Value=textBox3.Text;
sqlcom.Parameters.Add("@Productjj",SqlDbType.VarChar,100);
sqlcom.Parameters["@Productjj"].Value=textBox4.Text;
sqlcom.Parameters.Add("@Particular",SqlDbType.Text,16);
sqlcom.Parameters["@Particular"].Value=richTextBox1.Text;
sqlcom.Parameters.Add("@Pic",SqlDbType.Image,16);
sqlcom.Parameters["@Pic"].Value=theData;
//sqlcom.Prepare();
da.InsertCommand=sqlcom;
*/
da.Fill(ds,"MyImages");
DataRow myRow;
myRow=ds.Tables["MyImages"].NewRow();
myRow["Productdh"] = productdh;
myRow["Productlb"] = e1;
myRow["sccs"] = textBox3.Text;
myRow["Productjj"] =textBox4.Text;
myRow["Particular"] =richTextBox2.Text;
myRow["Addtime"]=DateTime.Now.ToLongTimeString();
myRow["Productlbming"]=textBox1.Text;
try
{
if(theData.Length>0)
{
myRow["Pic"] =theData;
}
else
{
myRow["Pic"]=null;
}
}
catch{}
try

ds.Tables["MyImages"].Rows.Add(myRow);


da.Update(ds, "MyImages");
//da.InsertCommand.ExecuteNonQuery();
MessageBox.Show("录入产品成功");

解决方案 »

  1.   

    zhehui(小慧)说的是什么意思?
    哪来的数据库连接呢?
      

  2.   

    注:MyTools.g_PhotoField为数据库表中的图象字段名称
    //将图片保存到数据库中
    if(this.picPhoto.Image==null)
    {
    m_DataRow[MyTools.g_PhotoField]=DBNull.Value;
    }
    else
    {
    try 
    {
    MemoryStream ms = new MemoryStream ();
    picPhoto.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
    byte [] myData = new Byte [ms.Length ];
    ms.Position = 0;
    ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
    m_DataRow[MyTools.g_PhotoField] = myData; }
    catch(System.Exception ee) 
    {
    MessageBox.Show(ee.Message);
    }
    }//else//读取图象
    if(this.m_DataRow[MyTools.g_PhotoField]!=DBNull.Value)
    {
    try
    {
    Byte[] byteBLOBData =  new Byte[0];
    byteBLOBData = (Byte[])m_DataRow[MyTools.g_PhotoField];
    MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
    this.picPhoto.Image= Image.FromStream(stmBLOBData);
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }
    else
    {
    this.picPhoto.Image= null;
    }