wmf 等文件的数据库保存和读取

其中 new byte[sm.Length+8]为:

 +8是wmf文件

 +1是office07文件

 jpg文件不加.

上面的规律是通过二进制解读器比较发现的.至于原因还米搞清楚.

 

程序包括两个函数:保存函数和读取函数.

数据库是在本机上建立一个名为printtest的数据库,在数据库里建一个表imagetest:

    id   int类型  (是标识)

    imagetest image类型

 

 

  using  System;
 
using  System.Windows.Forms;
 
using  System.Drawing;
 
using  System.Data.SqlClient;
 
using  System.Data;
 
using  System.IO;
 
using  System.Drawing.Imaging;
 
namespace  wmftest
ExpandedBlockStart.gifContractedBlock.gif 
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**//**//// <summary>
    
/// wmftest 的摘要说明。
    
/// </summary>

    public class wmftest 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
public wmftest()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
//private Point Corner_LeftTop;//定义左上角
        private static string sqlconnstr=
       
"Data Source=(local) ;Initial Catalog=printtest;Integrated Security=SSPI";
        
public static void InsertIMG()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

            
//将需要存储的图片读取为数据流
            FileStream sm=new FileStream(@"E:\1.wmf", FileMode.Open,FileAccess.Read);
            Byte[] byte2 
= new byte[sm.Length+8];
            sm.Read(byte2 , 
0, Convert.ToInt32(sm.Length));            
            sm.Close();
            
            
using (SqlConnection conn = new SqlConnection(sqlconnstr))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//数据库写入#region//数据库写入
                    conn.Open();
                    SqlCommand cmd 
= new SqlCommand();
                    cmd.Connection 
= conn;
                    cmd.CommandText 
= "insert into imagetest (imagetest) values (@par) ";
                    cmd.CommandType
=CommandType.Text;
                    cmd.Parameters.Add(
"@par",SqlDbType.Image);
                    cmd.Parameters[
"@par"].Value=byte2;
                    cmd.ExecuteNonQuery();
                    
#endregion
                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
catch(Exception ex){ MessageBox.Show(ex.Message);}
                conn.Close();
            }

        }

        
public static void Read(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
byte[] MyData = new byte[0];
            
using (SqlConnection conn = new SqlConnection(sqlconnstr))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                FileStream fs 
= new FileStream( @"c:\00.wmf", FileMode.Create, FileAccess.Write);
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//数据库读取#region//数据库读取
                    conn.Open();
                    SqlCommand cmd 
= new SqlCommand();
                    cmd.Connection 
= conn;
                    cmd.CommandText 
= "select imagetest from imagetest where id=@id";
                    cmd.CommandType
=CommandType.Text;
                    cmd.Parameters.Add(
"@id",SqlDbType.Int);//增加参数id,表示读取的是哪条记录
                    cmd.Parameters["@id"].Value=id;
                    SqlDataReader sdr 
= cmd.ExecuteReader();
                    sdr.Read();
                    
#endregion
                    
//数据流还原文件
                    MyData = (byte[])sdr["imagetest"];//读取第一个图片的位流
                    conn.Close();
                    
//获得数据库中存储的位流数组的维度上限,用作读取流的上限
                    int ArraySize= MyData.GetUpperBound(0);
                    fs.Write( MyData, 
0, ArraySize);
                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
catch(Exception ex){MessageBox.Show(ex.Message);}
                fs.Close();
//关闭
            }

        }

    }

}

转载于:https://www.cnblogs.com/yuiely/archive/2008/08/14/1268097.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值