ACCESS的Ole对象读取写入

Ole对象在Access中存储为二进制文件,读取的时候需要注意转换出的文件的编码格式

 1 None.gif OleDbConnection OleConn  =   new  OleDbConnection();
 2 None.gifOleConn.ConnectionString  =
@"
Provider=Microsoft.Jet.OleDb.4.0;data source=D:\WorkStation\Dialy_Sol\Dialy\Dialy.mdb " ;
 3 None.gifOleDbCommand OleCmd  =   new  OleDbCommand();
 4 None.gifOleCmd.Connection  =  OleConn;
 5 None.gifOleCmd.CommandType  =  CommandType.Text;
 6 None.gifOleCmd.CommandText  =
"
SELECT Dialy_Content FROM Dialy_Info WHERE Dialy_Date='2008-5-2' " ;
 7 None.gif if  (OleConn.State  ==  ConnectionState.Closed)
 8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 9InBlock.gifOleConn.Open();
10ExpandedBlockEnd.gif}

11 None.gif string  DialyContent  =   "" ;
12 None.gif byte [] Buff  =   new   byte [ 1000 ];
13 None.gifOleDbDataReader OleReader  =  OleCmd.ExecuteReader();
14 None.gif while  (OleReader.Read())
15 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
16InBlock.gifOleReader.GetBytes(00, Buff, 01000);
17InBlock.gifDialyContent += Encoding.Unicode.GetString(Buff);
18ExpandedBlockEnd.gif}

< /span>19< span style="color: #000000;">None.gif
// 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();
}
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值