C# 读写ACCESS的OLE对象,演示图片与长文件的读写

网络上的读写OLE对象的代码是多,可是多是转发的,半数以上人历来都没实际测试过,只是COPY来COPY去。小编重来没见到1个真正得以运作的东东。
不可能,唯有自力更生,花了一点岁月出了点商讨成果,写到这里做个记录。

重点代码如下:

                //pictureBox1.Image = Image.FromFile(fileName);

                OleDbConnection cn = new
OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” +
Application.StartupPath + “\\db1.mdb” + “;Persist
Security Info=True”);
                cn.Open();

******* void button1_Click(object sender, 伊芙ntArgs e) 
//写入图片
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = “All   Files|*.*”;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string fileName = dlg.FileName;

            OleDbDataAdapter oda = new OleDbDataAdapter(“select  pic 
from  list1 where 编号=12”, cn);

                FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

        ******* void button4_Click(object sender, 伊夫ntArgs e) 
//载入长文本
        {
            OleDbConnection cn = new
OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” +
Application.StartupPath + “\\db1.mdb” + “;Persist
Security Info=True”);
            cn.Open();

            byte[] buffer = dt.Rows[0][0] as byte[];
            //MemoryStream ms = new MemoryStream(buffer);
            richTextBox1.Text
=System.Text.Encoding.Default.GetString(buffer);

                OleDbCommand cmd = new OleDbCommand(“INSERT INTO 
list1(pic)  VALUES(@img)”, cn);
                ((OleDbParameter)cmd.Parameters.Add(“@img”,
OleDbType.Binary)).Value = buffer;
                cmd.ExecuteNonQuery();
            }  
        }

            cn.Close();

        ******* void button3_Click(object sender, 伊芙ntArgs e) 
//写入长文本
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = “TXT Files|*.txt”;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string fileName = dlg.FileName;

            byte[] buffer = dt.Rows[ACCESS,0][0] as byte[];
            MemoryStream ms = new MemoryStream(buffer);

                FileStream fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

        ******* void button2_Click(object sender, 伊夫ntArgs e) 
//读取图片
        {
            OleDbConnection cn = new
OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” +
Application.StartupPath + “\\db1.mdb” + “;Persist
Security Info=True”);
            cn.Open();

ACCESS 1

                //pictureBox1.Image = Image.FromFile(fileName);

        }

            DataTable dt = new DataTable();
            oda.Fill(dt);

                OleDbCommand cmd = new OleDbCommand(“INSERT INTO 
list1(pic)  VALUES(@img)”, cn);
                ((OleDbParameter)cmd.Parameters.Add(“@img”,
OleDbType.Binary)).Value = buffer;
                cmd.ExecuteNonQuery();
            }  
 

            pictureBox1.Image = Image.FromStream(ms);
        }

          
    }

            cn.Close();

            DataTable dt = new DataTable();
            oda.Fill(dt);

            OleDbDataAdapter oda = new OleDbDataAdapter(“select  pic 
from  list1 where 编号=10”, cn);
           

                OleDbConnection cn = new
OleDbConnection(“Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” +
Application.StartupPath + “\\db1.mdb” + “;Persist
Security Info=True”);
                cn.Open();

相关文章