ACCESSACCESS的Ole对象读博写副

Ole对象在Access中蕴藏吗二进制文件,读取的时刻要注意变换出之文书之编码格式

 1ACCESS 1OleDbConnection OleConn = new OleDbConnection();
 2ACCESS 2OleConn.ConnectionString =

@”Provider=Microsoft.Jet.OleDb.4.0;data source=D:\WorkStation\Dialy_Sol\Dialy\Dialy.mdb”;
 3ACCESS 3OleDbCommand OleCmd = new OleDbCommand();
 4ACCESS 4OleCmd.Connection = OleConn;
 5ACCESS 5OleCmd.CommandType = CommandType.Text;
 6ACCESS 6OleCmd.CommandText =

“SELECT Dialy_Content FROM Dialy_Info WHERE Dialy_Date=’2008-5-2′”;
 7ACCESS 7if (OleConn.State == ConnectionState.Closed)
 8ACCESS 8ACCESS 9ACCESS 10{
 9ACCESS 11OleConn.Open();
10ACCESS 12}
11ACCESS 13string DialyContent = “”;
12ACCESS 14byte[] Buff = new byte[1000];
13ACCESS 15OleDbDataReader OleReader = OleCmd.ExecuteReader();
14ACCESS 16while (OleReader.Read())
15ACCESS 17ACCESS 18ACCESS 19{
16ACCESS 20OleReader.GetBytes(0, 0, Buff, 0, 1000);
17ACCESS 21DialyContent += Encoding.Unicode.GetString(Buff);
18ACCESS 22}
< /span>19< span
style=”color:
#000000;”>ACCESS 23 //DialyContent就是读取出来后底中文

—————————下面是读取写副的以身作则———————————

******* OleDbConnection conn;
public Form1()
{
InitializeComponent();
conn = new OleDbConnection();
conn.ConnectionString = @”Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=d:\Database1.mdb”;
conn.Open();

}

//it is for storing a file into database
******* void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open,
FileAccess.Read);
Byte[] buff = new Byte[fs.Length];
BinaryReader rd = new BinaryReader(fs);
rd.Read(buff, 0, Convert.ToInt32(fs.Length));

OleDbCommand command = new OleDbCommand(“INSERT INTO 表1 ([object],
path) VALUES(@object, @path)”, conn);
command.Parameters.Add(“@object”, OleDbType.Binary, buff.Length).Value =
buff;
command.Parameters.Add(“@path”, OleDbType.Char, 255).Value =
openFileDialog1.FileName;
command.ExecuteNonQuery();
rd.Close();
fs.Close();

}

}

******* void Form1_FormClosed(object sender, FormClosedEventArgs
e)
{
conn.Close();
}
//it is for reading the data from the database and store it to a file
******* void button2_Click(object sender, EventArgs e)
{
OleDbCommand command = new OleDbCommand(“SELECT [object], path FROM
表1”, conn);
OleDbDataReader dr = command.ExecuteReader();
FileStream fs;
BinaryWriter writer;
int bufferSize = 100;
byte[] outByte = new byte[bufferSize];
while(dr.Read())
{
string filename = dr.GetString(1);
fs = new FileStream(filename, FileMode.OpenOrCreate,
FileAccess.Write);
writer = new BinaryWriter(fs);
int startIndex = 0;
long retval = dr.GetBytes(0, startIndex, outByte, 0, bufferSize);
while (retval == bufferSize)
{
writer.Write(outByte);
writer.Flush();

startIndex += bufferSize;
retval = dr.GetBytes(0, startIndex, outByte, 0, bufferSize);
}

writer.Write(outByte, 0, (int)retval – 1);
writer.Flush();

writer.Close();
fs.Close();

}
dr.Close();
}
 

相关文章