Inserting file into blob

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);
arichexeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Joel CoehoornConnect With a Mentor Director of Information TechnologyCommented:
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

0
 
Joel CoehoornDirector of Information TechnologyCommented:
I missed a \ character on the second line- you don't need to escape them in VB.
0
All Courses

From novice to tech pro — start learning today.