01.
Public
Function
SaveFile(
ByVal
clientFilePath
As
String
,
ByVal
clientFileExt
As
String
)
As
Integer
02.
03.
Dim
targetFileName
As
String
=
"f_"
& DateTime.Now.ToString(
"yyyyhMMdd_hmmss"
) & clientFileExt
04.
Dim
objReader
As
SqlDataReader
05.
Dim
objCmd
As
New
SqlCommand
06.
Dim
resultDt
As
DataTable =
New
DataTable()
07.
Dim
new_id
As
Integer
= 0
08.
09.
10.
Dim
o
As
FileStream
11.
12.
Dim
r
As
StreamReader
13.
14.
15.
o =
New
FileStream(clientFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
16.
17.
18.
r =
New
StreamReader(o)
19.
20.
Try
21.
22.
Dim
FileByteArray(o.Length - 1)
As
Byte
23.
24.
25.
o.Read(FileByteArray, 0, o.Length)
26.
27.
28.
Dim
strSQL
As
String
=
"INSERT INTO tbl_file ([file_name], [file_byte], [file_ext], [uploader], [upload_date]) VALUES ('"
& targetFileName &
"', @PictureL, '"
& clientFileExt &
"', '', '"
& DateTime.Now.ToString(
"dd/MM/yyyy"
) &
"')"
29.
objCmd =
New
SqlCommand(strSQL, objConn)
30.
31.
32.
objCmd.Parameters.Add(
"@PictureL"
, System.Data.OleDb.OleDbType.Binary, o.Length).Value = FileByteArray
33.
34.
35.
Dim
rowEffect
As
Integer
= objCmd.ExecuteNonQuery()
36.
objCmd.Dispose()
37.
38.
39.
If
(rowEffect > 0)
Then
40.
strSQL =
"SELECT @@identity AS NewID FROM tbl_file"
41.
objCmd =
New
SqlCommand(strSQL, objConn)
42.
43.
objReader = objCmd.ExecuteReader()
44.
resultDt.Load(objReader)
45.
46.
new_id =
CInt
(resultDt.Rows(0)(
"NewID"
))
47.
objCmd.Dispose()
48.
End
If
49.
50.
51.
objConn.Close()
52.
53.
Catch
ex
As
Exception
54.
ErrorMessage = ex.Message
55.
End
Try
56.
57.
58.
Return
new_id
59.
End
Function