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

ACCESS 1

纱直达之朗诵写OLE对象的代码是多,不过基本上是转载的,大部分人从还尚未实际测试了,只是COPY来COPY去。我重来没看一个真的好运作的东东。
没有道,只有自力更生,花了一些岁月有了接触研究成果,写及此处召开个记录。

最主要代码如下:

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

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

                //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();

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

          
    }

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

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

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

            cn.Close();

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

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

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

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

                //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();

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

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

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

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

            cn.Close();

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

        }

相关文章