.net C#,access,MDB数据库BLOB二进制字段的读写

     BLOB二进制字段读写,一致是一个十分麻烦的事情,本人在总结前人的基础,特对access,MDB数据库BLOB的读写总结如下:

 


using  System.Data;
using  System.Data.OleDb;
using  System.Windows.Forms;
using  System.IO;

namespace  Book
{
    
///  
    
/// Db_Class 的摘要说明。
    
/// 
 

    public class Db_Class
    
{
        
private OleDbConnection Conn;
        
private OleDbCommand cmd;
        
//构造函数
        public Db_Class(String MDBFileName, String Password)
        
{
          
            String Str 
= string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet Oledb:Database Password={1}", MDBFileName, Password);
            Conn 
= new OleDbConnection(Str);  //连接数据库,Password是MDB的密码
            
//by 闫磊 Email:[email protected],[email protected] 2007.10.13

        }

        
//打开数据源链接
        public OleDbConnection Db_Conn()
        
{
            Conn.Open();
            
return Conn;
        }

        
//返回DataReader数据集,下面的SQL可以动态生成
        public OleDbDataReader Db_CreateReader(string SQL)
        
{
            
try
            
{
                cmd 
= new OleDbCommand(SQL, Conn);
                OleDbDataReader Rs 
= cmd.ExecuteReader();

                
return Rs;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
"错误:因" + ex.Message + ",无法执行:" + SQL);
                
return null;

            }

        }

        
//返回DataReader数据集,下面的SQL是存储过程
        public OleDbDataReader Db_CommandReader(string SQL)
        
{
            
try
            
{

                cmd 
= new OleDbCommand(SQL, Conn);
                cmd.CommandType 
= CommandType.StoredProcedure;
                OleDbDataReader Rs 
= cmd.ExecuteReader();
                
return Rs;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
"错误:因" + ex.Message + ",无法执行:" + SQL);
                
return null;
            }


        }

        
//返回数据DataSet数据集
        public DataSet Db_CreateDataSet(string SQL)
        
{
            
try
            
{
                cmd 
= new OleDbCommand(SQL, Conn);
                OleDbDataAdapter Adpt 
= new OleDbDataAdapter(cmd);
                DataSet Ds 
= new DataSet();
                Adpt.Fill(Ds, 
"NewTable");
                
return Ds;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
"错误:因" + ex.Message + ",无法执行:" + SQL);
                
return null;
            }


        }

        
//返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数
        public bool Db_ExecuteNonquery(string SQL)
        
{
            
try
            
{
                Db_Conn();
                cmd 
= new OleDbCommand(SQL, Conn);
                
try
                
{
                    cmd.ExecuteNonQuery();

                    
return true;
                }

                
catch (Exception ex)
                
{
                    MessageBox.Show(
"错误:因" + ex.Message + ",无法执行:" + SQL);
                    
return false;
                }

            }

            
finally
            
{

                
this.close();
            }


        }

        
//返回数据DataReader数据集,不需要返回数据的修改,删除可以使用本函数
        public bool Db_ExecuteNonquery(string SQL, bool Connect)
        
{
            
try
            
{
                
if (Connect)
                    Db_Conn();
                cmd 
= new OleDbCommand(SQL, Conn);
                
try
                
{
                    cmd.ExecuteNonQuery();

                    
return true;
                }

                
catch (Exception ex)
                
{
                    MessageBox.Show(
"错误:因" + ex.Message + ",无法执行:" + SQL);
                    
return false;
                }

            }

            
finally
            
{
                
if (Connect)
                    
this.close();
            }


        }

        
//返回数据DataReader数据集,返回数据
        public String Db_Executequery(string SQL)
        
{
            
try
            
{
                Db_Conn();
                cmd 
= new OleDbCommand(SQL, Conn);
                
try
                
{
                    
return cmd.ExecuteScalar().ToString();


                }

                
catch (Exception ex)
                
{
                    MessageBox.Show(
"错误:因" + ex.Message + ",无法执行:" + SQL);
                    
return "";
                }

            }

            
finally
            
{
                
this.close();
            }


        }

        
//返回数据DataReader数据集,返回数据
        public String Db_Executequery(string SQL,String parent, bool connect)
        
{
            
try
            
{
                
if (connect)
                    Db_Conn();
                cmd 
= new OleDbCommand(SQL, Conn);
                
try
                
{
                    
return cmd.ExecuteScalar().ToString();


                }

                
catch (Exception ex)
                
{
                    MessageBox.Show(
"parent:" + parent + "错误:因" + ex.Message + ",无法执行:" + SQL);
                    
return "";
                }

            }

            
finally
            
{
                
if (connect)
                    
this.close();
            }


        }

        
//返回blob数据
        public MemoryStream getBlob(string SQL)
        
{
            
try
            
{
                Db_Conn();
                cmd 
= new OleDbCommand(SQL, Conn);
                cmd.CommandType 
= CommandType.Text;//是sql
                OleDbDataReader Rs = cmd.ExecuteReader();
                
if (Rs.Read()) //循环到下一条记录
                {
                    
if (!(Rs.GetValue(0)  is System.DBNull))
                    
{
                        
byte[] image_bytes = (byte[])Rs.GetValue(0);
                        MemoryStream ms 
= new MemoryStream(image_bytes);
                        
return ms;
                    }

                    
else
                        
return null;
                }

                
else
                    
return null;

            }

           
            
finally
            
{
                
this.close();
            }



        }

        
//设置blob
        public bool SetBlob(string SQL, MemoryStream Ms)
        
{
            
try
            
{
                Db_Conn();
               
                cmd 
= new OleDbCommand(SQL, Conn);              
                cmd.CommandType 
= CommandType.Text;//是sql
         
                
int n=Convert.ToInt32(Ms.Length.ToString());
                Ms.Position 
= 0;
                
byte[] pReadByte = new Byte[n];
                Ms.Read(pReadByte, 
0, n);              

                cmd.Parameters.Add(
"BLOB", OleDbType.Binary).Value = pReadByte;
                cmd.ExecuteNonQuery();

                
return true;

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
"错误:因" + ex.Message + ",无法执行:" + SQL);
                
return false;
            }


            
finally
            
{
                
this.close();
            }



        }



        
//关闭数据链接
        public void close()
        
{
            Conn.Close();
        }


    }

}

 

说明:

1、getBlob(string SQL)是读BLOB字段,调用例子如下

 

            // by 闫磊 Email:[email protected],[email protected] 2007.10.23 
           String sqlStr  =   " select content from dp where id= "   +  ID; // content为dp中的BLOB字段,ID为主键
            MemoryStream ms  =  DBClass.getBlob(sqlStr);
            
if  (ms  ==   null )
               richTextBox.Clear();
            
else
            
{
                
if (ms.Length > 0)
                
{
                    ms.Position 
= 0;
                    
try
                    
{
                        richTextBox.LoadFile(ms, RichTextBoxStreamType.RichText);
                    }

                    
catch
                    
{
                        richTextBox.LoadFile(ms, RichTextBoxStreamType.PlainText);
                    }

                }

                
else
                    richTextBox.Clear();
            }

2、BLOB二进制的写,SetBlob(string SQL, MemoryStream Ms),调用如下

 

private   void  SaveData( int  ID1)
        
{
            String sqlStr 
= "update dp set content=:BLOB where id=" + ID;
            MemoryStream ms 
= new MemoryStream();
            richTextBox.SaveFile(ms, RichTextBoxStreamType.RichText);
            
if (!DBClass.SetBlob(sqlStr, ms))
            
{
                MessageBox.Show(
"保存失败");
            }


        }

 

 

你可能感兴趣的:(.net C#,access,MDB数据库BLOB二进制字段的读写)