We help IT Professionals succeed at work.

Inserting file into blob

arichexe
arichexe asked
on
The below C# snippet loads a .doc into a SQL image.  How would I code such in VB.NET?

FileStream MyStream = new FileStream("C:\\My.doc",FileMode.Open);
int DocLen = (int)new FileInfo("C:\\My.doc").Length;
byte[] MyBlob = new byte[DocLen];
int n = MyStream.Read(MyBlob,0,DocLen);

string MySql = "INSERT INTO MyBlobTable (MyId,MyBlob) VALUES (1,@MyBlob)";
SqlCommand sql = new SqlCommand(MySql,MyConn);
SqlParameter sqlp = new SqlParameter("@MyBlob",SqlDbType.Image);
sqlp.Value = MyBlob;
sql.Parameters.Add(sqlp);
Comment
Watch Question

Director of Information Technology
BRONZE EXPERT
Commented:
This is just a direct translation of the code your provided.  It depends on the same Imports (like System.IO) and supporting objects (there a MyConn variable in the original who's source is not explained):
Dim MyStream As New FileStream("C:\My.doc",FileMode.Open)
Dim DocLen As Integer = New FileInfo("C:\\My.doc").Length
Dim MyBlob(DocLen) As Byte
Dim n As Integer = MyStream.Read(MyBlob,0,DocLen)
 
Dim MySql As Sting = "INSERT INTO MyBlobTable (MyId,MyBlob) VALUES (1,@MyBlob)"
Dim sql As New SqlCommand(MySql,MyConn)
Dim sqlp As New SqlParameter("@MyBlob",SqlDbType.Image)
sqlp.Value = MyBlob
sql.Parameters.Add(sqlp)

Open in new window

Joel CoehoornDirector of Information Technology
BRONZE EXPERT

Commented:
I missed a \ character on the second line- you don't need to escape them in VB.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.