C# 将图片存入数据库中

SQL Server2000

private void button1_Click(object sender, System.EventArgs e)
  { 
   openFileDialog1.ShowDialog ();

   if (openFileDialog1.FileName.Trim()!="")
   {
    FileInfo fi = new FileInfo(openFileDialog1.FileName);
    string imgtitle=openFileDialog1.FileName;
    int imgdatalen=(int)fi.Length;  
    byte[] imgdata = new byte[imgdatalen];
    Stream imgdatastream=fi.OpenRead();
    int n=imgdatastream.Read(imgdata,0,imgdatalen);
    if( conn.State == ConnectionState.Open)
     conn.Close(); 
    ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=mydb;" +"Data Source=localhost;";       
    conn.ConnectionString = ConnectionString;
   
    try
    {
     string mySelectQuery = "INSERT INTO ImageStore(imgdata) VALUES (@imgdata )";
  
     SqlCommand myCommand = new SqlCommand(mySelectQuery, conn);
     SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
     paramData.Value = imgdata;
     myCommand.Parameters.Add( paramData );

     conn.Open(); 
     int numRowsAffected = myCommand.ExecuteNonQuery();
     conn.Close();

     MessageBox.Show("成功放入图片到数据库中");
    }
    catch(Exception err)
    {
     MessageBox.Show("您输入名称可能在数据库中已存在或输入为空,请检查!"+err.ToString() );
    }
    finally
    {}

   
   }

  }

 

  private void button2_Click(object sender, System.EventArgs e)
  {
   if( conn.State == ConnectionState.Open)
    conn.Close();
   ConnectionString ="Integrated Security=SSPI;" + "Initial Catalog=mydb;" +"Data Source=localhost;";       
   conn.ConnectionString = ConnectionString;
   string sql="SELECT *  FROM ImageStore where ;
   SqlCommand command = new SqlCommand(sql, conn);
     try
   {
    conn.Open();
   }
   catch
   {
    MessageBox.Show(" 不能打开数据联接!") ;
   }
              
  
   SqlDataReader dr = command.ExecuteReader();
  
   if(dr.Read())
   {
   
    byte[] mydata=((byte[])dr["imgdata"]);
    MemoryStream myStream=new MemoryStream();
    foreach(byte a in mydata)
    {
     myStream.WriteByte(a);
    }
    Image myImage=Image.FromStream(myStream);
    myStream.Close();
               pic1.Image=myImage;
    pic1.Refresh();
   
   }
   else
   {
    MessageBox.Show("没有成功读入数据!") ;
  
   }
  conn.Close();

Access部分

private void button1_Click(object sender, System.EventArgs e)

  { 

   openFileDialog1.ShowDialog ();

   if (openFileDialog1.FileName.Trim()!="")

   {

    FileInfo fi = new FileInfo(openFileDialog1.FileName);

    string imgtitle=openFileDialog1.FileName;

    int imgdatalen=(int)fi.Length;  

    byte[] imgdata = new byte[imgdatalen];

    Stream imgdatastream=fi.OpenRead();

    int n=imgdatastream.Read(imgdata,0,imgdatalen);

                string strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"//mydb.mdb";

    OleDbConnection conn = new OleDbConnection ( strCon ) ;

    try

    {

     string mySelectQuery = "INSERT INTO ImageStore(imgdata) VALUES (@imgdata )";

     OleDbCommand myCommand = new OleDbCommand(mySelectQuery, conn);

     OleDbParameter paramData = new OleDbParameter( "@imgdata", SqlDbType.Image );

     paramData.Value = imgdata;

     myCommand.Parameters.Add( paramData );

     conn.Open(); 

     int numRowsAffected = myCommand.ExecuteNonQuery();

     conn.Close();

     MessageBox.Show("成功放入图片到数据库中");

    }

    catch(Exception err)

    {

     MessageBox.Show("您输入名称可能在数据库中已存在或输入为空,请检查!"+err.ToString() );

    }

    finally

    {}

   }

  }

 

    private void button2_Click(object sender, System.EventArgs e)

    {

     string strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"//mydb.mdb";

     OleDbConnection conn = new OleDbConnection ( strCon ) ;

     if(textBox1.Text.Trim()=="") MessageBox.Show("显示不能为空!");

     else

     {

      string sql="SELECT *  FROM ImageStore where ;

      OleDbCommand command = new OleDbCommand(sql, conn);

      try

      {

       conn.Open();

      }

      catch

      {

       MessageBox.Show(" 不能打开数据联接!") ;

      }

      OleDbDataReader dr = command.ExecuteReader();

  

      if(dr.Read())

      {

       byte[] mydata=((byte[])dr["imgdata"]);

       MemoryStream myStream=new MemoryStream();

       foreach(byte a in mydata)

       {

        myStream.WriteByte(a);

       }

       Image myImage=Image.FromStream(myStream);

       myStream.Close();

       pic1.Image=myImage;

       pic1.Refresh();

      }

      else

      {

       MessageBox.Show("没有成功读入数据!") ;

      }

      conn.Close();

     }

上面内容为程序片段,如有不明白可以留言。

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值