Thursday, September 4, 2008

Storing and Downloading ms word file in ms access file using VB.NET 2.0

Storing a ms word file in access database using VB.NET 2.0


Dim objcon As System.Data.OleDb.OleDbConnection = CommonFunctions.clsCommonFunctions.OleDbConnections()

Dim objcmd As System.Data.OleDb.OleDbCommand = New OleDb.OleDbCommand("", objcon)

Dim par As OleDb.OleDbParameter = New OleDb.OleDbParameter("@Attachment", OleDb.OleDbType.LongVarBinary)

Dim strText As String = "update bonds set Attachment=@Attachment where bondsid=" & txtDBaseRefNo.Text

objcon.Open()

Dim fs As New System.IO.FileStream _

(TextBox1.Text, System.IO.FileMode.OpenOrCreate, _

System.IO.FileAccess.Read)

Dim MyData(fs.Length) As Byte

fs.Read(MyData, 0, fs.Length)

fs.Close()

par.Value = MyData

objcmd.CommandText = strText

objcmd.Parameters.Add(par)

Dim i As Integer = objcmd.ExecuteNonQuery()

fs = Nothing

objcon.Close()


Downloading ms word file from ms access database using VB.NET 2.0


Dim objcon As System.Data.OleDb.OleDbConnection = CommonFunctions.clsCommonFunctions.OleDbConnections()

Dim command As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand( _

"select Attachment from bonds where bondsid=" & txtDBaseRefNo.Text, objcon)

' Writes the BLOB to a file (*.bmp).

Dim stream As System.IO.FileStream

' Streams the binary data to the FileStream object.

Dim writer As System.IO.BinaryWriter

' The size of the BLOB buffer.

Dim bufferSize As Integer = 100

' The BLOB byte() buffer to be filled by GetBytes.

Dim outByte(bufferSize - 1) As Byte

' The bytes returned from GetBytes.

Dim retval As Long

' The starting position in the BLOB output.

Dim startIndex As Long = 0

' The publisher id to use in the file name.

Dim pubID As String = ""

' Open the connection and read data into the DataReader.

objcon.Open()

Dim reader As System.Data.OleDb.OleDbDataReader = command.ExecuteReader(CommandBehavior.SequentialAccess)

Do While reader.Read()

' Get the publisher id, which must occur before getting the logo.

'pubID = reader.GetString(0)

' Create a file to hold the output.

stream = New System.IO.FileStream( _

"C:\Documents and Settings\Gopinath\Desktop\doccc1111.doc", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)

writer = New System.IO.BinaryWriter(stream)

' Reset the starting byte for a new BLOB.

startIndex = 0

' Read bytes into outByte() and retain the number of bytes returned.

retval = reader.GetBytes(0, startIndex, outByte, 0, bufferSize)

' Continue while there are bytes beyond the size of the buffer.

Do While retval = bufferSize

writer.Write(outByte)

writer.Flush()

' Reposition start index to end of the last buffer and fill buffer.

startIndex += bufferSize

retval = reader.GetBytes(0, startIndex, outByte, 0, bufferSize)

Loop

' Write the remaining buffer.

writer.Write(outByte, 0, retval - 1)

writer.Flush()

' Close the output file.

writer.Close()

stream.Close()

Loop

' Close the reader and the connection.

reader.Close()

objcon.Close()




No comments: